26
Thu, Dec
0 New Articles

TechTip: Embrace Auto-Updating IBM i Performance Stats in Real Time

Scripting
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Deliver real-time WRKACTJOB stats to the browser using Node.js, DB2 for i Services, and WebSockets.

 

I am continually intrigued by the capabilities HTML5's WebSockets afford us on IBM i. In the article Node.js Is Genius with Web Sockets, I walked through the creation of a simple chat application that used WebSockets to communicate messages from the server to each browser currently participating in the chat. That was fun but not as immediately relevant to IBM i as it could have been. In this article, we will implement WebSockets into the IBM i Dash open-source project by adding an auto-updating page that conveys information equivalent to the WRKACTJOB command.

 

As a side note, the most recent V7.2 TR3 IBM i announcement includes Apache WebSocket support.

 

The ease of accessing WRKACTJOB-type information on IBM i has been made incredibly simple because of IBM's growing ecosystem of DB2 for i Services. These services essentially make system information (formerly only accessible via complex API calls) very easy to obtain via a simple SQL statement. Below is an SQL SELECT statement that calls upon the ACTIVE_JOB_INFO() User-Defined Table Function (UDTF). If you're new to UDTFs, you should read this article by DB2 expert Birgitta Hauser that does an excellent job of introducing the subject matter.

 

SELECT

JOB_NAME,

AUTHORIZATION_NAME,

ELAPSED_TOTAL_DISK_IO_COUNT,

ELAPSED_CPU_PERCENTAGE

FROM TABLE( QSYS2.ACTIVE_JOB_INFO() ) X

ORDER BY ELAPSED_CPU_PERCENTAGE DESC

FETCH FIRST 20 ROWS ONLY

In short, UDTFs are used in similar fashion to RPG's service programs, which hide complexity and provide reusability. In this case, there are likely many things going on under the covers of ACTIVE_JOB_INFO, but we only care about the inbound and outbound parameters. For this example, we provide no input (which would allow us to further filter results) and instead specify result columns we need for the web page we're creating. The ORDER BY clause is sorting by ELAPSED_CPU_PERCENTAGE because one of my primary uses of WRKACTJOB is to see who's hogging the machine. I used the STRSQL 5250 command to test the above SQL and honed it to meet the needs of the web application.

 

Now that we've resolved the DB2 for i Service syntax, it's time to implement it within the existing IBM i Dash open-source project. Note that you can feel free to add your own dashboard pages by "forking" the project, as detailed on the IBM i Dash README.md file.

 

I used a Litmis Space to accomplish this feature, which required that I copy my SSH public key from my home directory to my Bitbucket.org profile so I could perform the below git clone command. You can get full details on how to obtain your SSH public key and place it on Bitbucket in this MC Press Online article.

 

The below two commands will copy the ibmidash project down to your development environment and place you into the immediate project's directory with the cd (change directory) command.

 

% git clone This email address is being protected from spambots. You need JavaScript enabled to view it.:litmis/ibmidash.git

% cd ibmidash

 

Note: whenever you see the percent symbol (%), it means you're in a PASE shell. An alternative is to use CALL QP2TERM to enter these commands.

 

Included in the ibmidash project is file package.json. This holds a variety of items, which include module dependencies for this projectin this case ExpressJs and Jade. We don't yet have those modules installed in this environment, so it's necessary to run npm install, as shown below.

 

% npm install

 

Next we need to install the socket.io module and save it to the package.json file using the below command.

 

% npm install socket.io --save

 

At this point, we have everything installed that's necessary to complete the programming. One of the nice things about storing code in git on Bitbucket is the ability to easily see changes done across an entire project to learn what was affected. Here is a link to the changes I made that we will now walk through.

 

Below is a screenshot of the changes made to index.js, in which red highlights denote removing lines and green adding lines. The first relevant change is on line 3, where the socket.io module is being brought into our program. Think of this as being similar to doing an RPG /copy to bring in outside functionality.

 

103015BartellFigure1

Figure 1: Git shows the changes to index.js.

 

The next change is adding route app.get('wrkactjob', …). This will render the views/wrkactjob.jade file to the browser. We'll cover its contents later. The key server-side bit of code is line 65 through 74. The setInterval function is a means to emit updates to WebSocket event listeners every two seconds (line 74 is 2000 milliseconds). The value being emitted is the result of the call to ACTIVE_JOB_INFO(), as detailed on line 68. Line 72 is where the emitting actually happens, and it's sending this message only for those clients listening for the particular event ID of 'wrkactjob_update'.

 

Figure 2 shows the changes to header.jade. In this case, we needed to include reference to the client-side socket.io code so it could be downloaded from the Content Delivery Network (CDN). A new menu item was also added so users can navigate to the new page. Note how the link in the anchor (a) tag matches the route in index.js we mentioned earlier so inbound requests will deliver the wrkactjob.jade file to the browser.

 

103015BartellFigure2

Figure 2: Git shows the changes for header.jade.

 

Figure 3 shows the new wrkactjob.jade that's delivered down to the browser. Of initial interest is the table being composed, which contains an empty tbody. That's because the app will subsequently fill it with data from the WebSocket after the page loads.

 

103015BartellFigure3

Figure 3: Git shows the changes of wrkactjob.jade.

 

Lines 12 through 24 are where the action happens with JavaScript embedded in the HTML document. On line 13, we're initializing the client-side socket.io library. On line 14, we're listening for an event of 'wrkactjob_update' being emitted from the server. Upon receiving an event from the server, jQuery will be used to iterate over the result set and concatenate a new set of table rows. Then $("#jobs tbody").html(tbl_body) is used to select and update the tbody tag's contents.

 

We've now seen the code changes, and it's time to see the application in action. Starting the application is as simple as running node index.js, as shown below.

 

% node index.js        

Connect to *LOCAL

Connected to *LOCAL with ID , password: .

listening on *:8000

SQL: SELECT JOB_NAME, AUTHORIZATION_NAME, ELAPSED_TOTAL_DISK_IO_COUNT, ELAPSED_CPU_PERCENTAGE FROM TABLE(QSYS2.ACTIVE_JOB_INFO()) X ORDER BY ELAPSED_CPU_PERCENTAGE DESC FETCH FIRST 20 ROWS ONLY

 

When the app is running, you'll see the SQL statement repeated in the console because database debugging is turned on at the top of index.js.

 

Figure 4 shows the application running. A still image doesn't really do it justice. The entire table actually updates every two seconds, which is pretty cool! At the bottom of Figure 4 is the Chrome developer console with the Elements tab displayed. In this case, the tbody and tr tags are highlighted because they were just recently updated.

 

103015BartellFigure4

Figure 4: iDash WRKACTJOB auto-updates the page.

 

In conclusion, there are undoubtedly many other DB2 for i Services that could make use of this same WebSocket scenario (e.g., NETSTAT_INFO). It's my hope that others in the community will take on my challenge to contribute additional features to this project!

 

What ideas do you have for enhancing the IBM i Dash project?

 

Let's start a conversation and talk through the possibilities! Reach me directly at This email address is being protected from spambots. You need JavaScript enabled to view it..

 

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: