With this blog I'd like to illustrate one way that a SAS administrator can programmatically generate session and usage reports. We know that it's possible to gather considerable information about a running CAS server, about such things as nodes in the deployment, resource usage, and in particular about users' sessions. Here I'm going to illustrate a few alternatives using the REST API, without the CAS Server Monitor. It turns out that there's a very simple REST API call that will return some basic data about all user sessions, including the following:
One of the most common needs of an administrator is to know who is using their system, and how heavily they are using it. There's rarely a complete or direct answer, but this example goes part way to helping answer that question, for the Viya platform.
Here's the format of the REST API call, and what it looks like to issue this request from the Linux command prompt. The
uID:password must be those of a CAS administrator:
curl -n -u uID:passwd -X GET http://< machine >:8777/cas/sessions
Notice that we receive a JSON file, with 15+ variables for each active session, as shown above.
Several of these fields, including the most interesting ones, are easily displayed using the CAS Server Monitor, for example:
However, there are times when you may want to access this data programmatically, for example, in a production environment you may want to audit usage data over a longer term, or you may want to monitor sessions that last too long in an idle state in order to terminate them. There are a few possible ways to do this--I'll illustrate one of them here, and point to some of the other possibilities.
The following simple SAS program does the following:
Here's the SAS code, which can be run in SAS Studio (or any other SAS client):
filename in url 'http://<myserver>:8777/cas/sessions' debug user='userID' pass='password';
filename map '/opt/sas/viya/config/user.map' ;
libname in json map=map automap=replace;
run;
data all;
merge in.root
in.idleTime;
proc print data=all;
title 'display of current sessions, showing length of time since last CAS action';
title2 'and total number of CAS actions run in this session';
var uuid name user actioncount isidle hours minutes seconds;
run;
Clearly this isn't perfect data, but it could be useful in indicating zombie sessions, and in pointing to overall usage by various users--for example, this could be run as a report once / day, and data sets created could be combined to get longer-term usage information about usage--something all administrators want to have.
Here's an example of an output table from the code listed above:
Explanations:
Other approaches might include the following:
1. Run the REST API command and pipe the output to a local file. Then process that file with SAS or another language to create a report needed.
2. Rather than use the JSON libname, it's possible to write your own SAS datastep to convert the JSON file to a SAS data set. One example is shown in this SGF paper, using the SCANOVER data step function:
http://support.sas.com/resources/papers/proceedings13/296-2013.pdf
3. Use GROOVY scripting language to process the JSON file, as illustrated in this SGF paper: http://support.sas.com/resources/papers/proceedings16/1660-2016.pdf
4. Use the DS2 (SAS) procedure to process the JSON file. Documentation is found here: https://communities.sas.com/t5/Base-SAS-Programming/Using-DS2-to-parse-JSON-from-a-REST-API/td-p/227...
Additional relevant documentation:
Article illustrating JSON libname from Enterprise Guide: https://communities.sas.com/t5/SAS-Enterprise-Guide/Reading-json-file-into-SAS-using-SAS-enterprise-...
Finally, the JSON libname is fully documented here: JSON Libref Documentation
The above document explains how you can modify the map file to re-structure the output data sets that are created.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.