Published on :

Querying SAS Workload Orchestrator jobs via API

This code is also available in: Deutsch English Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The script uses PROC HTTP to authenticate with SAS© Workload Orchestrator and obtain an authentication cookie. Then, it submits an authenticated request to the jobs API to retrieve a list of all non-archived jobs. The response, in JSON format, is read using a JSON-type library (libname). Finally, a PROC SQL step is used to join different parts of the JSON data into a single SAS© table, which is then displayed.
Data Analysis

Type : EXTERNE


Data is dynamically retrieved via a call to the SAS Workload Orchestrator REST API. The script does not depend on pre-existing data files or SASHELP tables.

1 Code Block
PROC HTTP
Explanation :
This block defines connection parameters (username, password, base URL) and initializes temporary files. It then performs a first PROC HTTP call with the POST method to authenticate to the SAS Grid interface and retrieve an authentication cookie in the temporary 'headout' file.
Copied!
1/* Provide connection information. */
2%let username = sas;
3%let pw = password;
4%let baseURL = http://sgmg-master.demo.sas.com:8901;
5 
6/* Initialize temporary files to capture HTTP response body and headers. */
7 
8filename body temp;
9filename headout temp;
10 
11/* Authenticate and get an auth cookie. */
12 
13PROC HTTP URL="&baseURL/sasgrid/index.html" method="post"
14out=body headerout=headout headerout_overwrite
15in="username=&username%nrstr(&password)=&pw";
16headers "ContentType"="application/x-www-form-urlencoded";
17RUN;
2 Code Block
PROC HTTP Data
Explanation :
This second PROC HTTP call uses the previously obtained authentication to query the jobs API. It sends a GET request to obtain all non-archived jobs ('state=ALL'). The API response, which is in JSON format, is written to the temporary 'body' file.
Copied!
1/* Submit an authenticated query against the jobs API */
2/* requesting jobs with a state of "ALL" e.g. any non-archived jobs. */
3 
4PROC HTTP URL="&baseURL/sasgrid/api/jobs?state=ALL"
5out=body headerout=headout headerout_overwrite;
6headers "Accept"="application/vnd.sas.sasgrid.jobs;version=1;charset=utf-8";
7RUN;
3 Code Block
LIBNAME Data
Explanation :
This block uses the LIBNAME JSON engine to interpret the content of the 'body' file. It creates a virtual SAS library named 'jobinfo' whose tables correspond to the objects found in the JSON file structure.
Copied!
1/* Deassign the jobinfo libname. */
2LIBNAME jobinfo;
3 
4/* Read in the job info. */
5LIBNAME jobinfo json fileref=body;
4 Code Block
PROC SQL Data
Explanation :
This SQL procedure creates a SAS table named 'jobs' in the WORK library. It performs a join on the tables generated from the JSON ('jobinfo.jobs', 'jobinfo.jobs_processinginfo', 'jobinfo.jobs_request') to consolidate relevant information about each job into a single flat table.
Copied!
1/* Create a single table with items of interest. */
2PROC SQL;
3create TABLE jobs as
4 select
5 a.id,
6 b.state, b.queue, b.submitTime, b.startTime,
7 b.endTime, b.processId, b.executionHost, b.exitCode,
8 c.name, c.user, c.cmd
9 from
10 jobinfo.jobs a,
11 jobinfo.jobs_processinginfo b,
12 jobinfo.jobs_request c
13 where
14 a.ordinal_jobs = b.ordinal_jobs and
15 b.ordinal_jobs = c.ordinal_jobs
16 order BY a.id;
17QUIT;
5 Code Block
PROC PRINT
Explanation :
Displays the content of the 'work.jobs' table created in the previous step, thus presenting the final job report.
Copied!
1/* Print it. */
2PROC PRINT DATA=work.jobs; RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Author: Greg Wootton Date: 30AUG2019


Banner
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« Using PROC HTTP to interface directly with the SAS Workload Orchestrator (SWO) microservices is a modern, scalable approach to grid administration. This method transforms your SAS session into a powerful monitoring console, allowing you to bypass the GUI to extract real-time data on queue states, node utilization, and job lifecycles. »