Blogs

Scripting How-To: Display CPU usage information graphically

By Erdem posted 08-12-2015 17:27

  

Overview

Cool display of CPU usage graphically. This applies to SLAX version 1.0 and higher.

 

Description

 

Easily see the CPU processes usage over the last 60 minutes for any router configured with this script. Graphical format makes visual analysis quick and easy. Advanced users can customize the script to view other information in chart format as well, such as router temperature or memory usage.

 

This script uses get-accounting-record-information to collect routing engine CPU usage for the last 60 minutes and displays it in a chart format. It can also be modified to display other information, such as router temperature, round trip time (RTT) details, memory usage, and so on.

 

Set Up

 

  1. Copy the file cpu-usage-60.slax to /var/db/scripts/ops directory on the router.
  2. Use a text editor to create the configuration below and copy it to the clipboard:
01	accounting-options {
02	    file router.prf {
03	        files 3;
04	}
05	    routing-engine-profile router-profile {
06	        file router.prf;
07	        interval 1;
08	        fields {
09	            time-of-day;
10	            total-cpu-usage;
11	        }
12	    }
13	}

 3. On the device, enter configuration mode:

1	user@host% cli
2	user@host> configure
3	Entering configuration mode
4	 
5	 
6	[edit]
7	user@host#

 4. Use the load merge terminal command to load the configuration and merge it with the current configuration:

1	user@host# load merge terminal
2	[Type ^D at a new line to end input]
3	 > Paste the contents of the clipboard here <

 5. At the prompt, paste the contents of the clipboard.

 6. Press Enter.

 7. Press Ctrl+D.

 8. Enable the script by using the file statement and script filename to the [edit system scripts op] hierarchy level in the configuration, as shown below. Only superusers can enable scripts in the configuration.

 

1	[edit system scripts op]
2	user@host# set file cpu-usage-60.slax

 9. Commit the configuration: 

1	[edit]
2	user@host# commit and-quit

 10. Commit the configuration:

1	[edit]
2	user@host# commit and-quit

 

How to Run

 

  1. Make sure at least 10 minutes have passed before executing this script for the first time, to allow enough time for data to be collected.
  2. Enter the command op cpu-usage-60 from the operational mode of CLI. The CPU usage for the last 60 minutes will be displayed in a graph similar to this:

 

01	user@host> op cpu-usage-60
02	 
03	                    1          1                     1
04	    999998999989998818889899888299989998999898989789907888889999
05	100| 
06	 90| 
07	 80| 
08	 70| 
09	 60| 
10	 50| 
11	 40| 
12	 30| 
13	 20|                *          * 
14	 10|**************** ********** ******************************** 
15	   +------------------------------------------------------------ 
16	   0....5....1....1....2....2....3....3....4....4....5....5....6 
17	             0    5    0    5    0    5    0    5    0    5    0
18	 
19	x-axis => Mins, y-axis => CPU%  (CPU Usage for the last 60 minutes) 
20	 
21	* show the CPU usage for the last 60 minutes
22	* the top 2 rows vertically display the CPU % of that minute
23	* the CPU % for the last minute is 9%

 

Source Code

 

Example Configuration

 

01	user@cli> show configuration accounting-options
02	file router.prf {
03	    files 3;
04	}
05	routing-engine-profile router-profile {
06	    file router.prf;
07	    interval 1;
08	    fields {
09	        time-of-day;
10	        total-cpu-usage;
11	    }
12	}
13	 
14	user@cli> show configuration system scripts op file cpu-usage-60.slax {
15	    command cpu_usage;
16	}

 

Example Output

 

01	copy the file cpu-usage-60.slax to /var/db/scripts/op and let the accounting-option colle
02	t date (wait 10+ minutes), then type of the following:
03	 
04	user@cli> op cpu-usage-60    
05	 
06	                     1          1                     1
07	     999998999989998818889899888299989998999898989789907888889999
08	 100| 
09	  90| 
10	  80| 
11	  70| 
12	  60| 
13	  50| 
14	  40| 
15	  30| 
16	  20|                *          * 
17	  10|**************** ********** ******************************** 
18	    +------------------------------------------------------------ 
19	    0....5....1....1....2....2....3....3....4....4....5....5....6 
20	              0    5    0    5    0    5    0    5    0    5    0
21	 
22	 x-axis => Mins, y-axis => CPU%  (CPU Usage for the last 60 minutes) 
23	 
24	 * show the CPU usage for the last 60 minutes
25	 * the top 2 rows vertically display the CPU % of that minute
26	 * the CPU % for the last minute is 9%

 

SLAX Script Contents

 

001	version 1.0;
002	 
003	ns junos = "http://xml.juniper.net/junos/*/junos";
004	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
005	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
006	ns ext = "http://xmlsoft.org/XSLT/namespace";
007	 
008	import "../import/junos.xsl";
009	 
010	/*
011	 * OpScript to monitor CPU usage
012	 *
013	 *   user@cli> show configuration accounting-options file router.prf {
014	 *       files 3;
015	 *   }
016	 *   routing-engine-profile router-profile {
017	 *       file router.prf;
018	 *       interval 1;
019	 *       fields {
020	 *           time-of-day;
021	 *           total-cpu-usage;
022	 *       }
023	 *   }
024	 *  
025	 *   user@cli> show configuration system scripts op file cpu-usage-60.slax {
026	 *       command cpu_usage;
027	 *   }
028	 *
029	 *   Copy the file to /var/db/scripts/op and let the accounting-option collect data
030	 *   (wait 10+ minutes), then type the following:
031	 *  
032	 *   user@cli> op cpu-usage-60
033	 *
034	 *                   1          1                     1
035	 *    999998999989998818889899888299989998999898989789907888889999
036	 * 100|
037	 *  90|
038	 *  80|
039	 *  70|
040	 *  60|
041	 *  50|
042	 *  40|
043	 *  30|
044	 *  20|                *          *
045	 *  10|**************** ********** ********************************
046	 *    +------------------------------------------------------------
047	 *    0....5....1....1....2....2....3....3....4....4....5....5....6
048	 *              0    5    0    5    0    5    0    5    0    5    0
049	 * x-axis => Mins, y-axis => CPU%  (CPU Usage for the last 60 minutes)
050	 *
051	 */
052	 
053	var $xaxis-line = "   +------------------------------------------------------------";
054	var $xaxis-num1 = "   0....5....1....1....2....2....3....3....4....4....5....5....6";
055	var $xaxis-num2 = "             0    5    0    5    0    5    0    5    0    5    0";
056	 
057	var $xaxis-num3 = "x-axis => Mins, y-axis => CPU%  (CPU Usage for the last 60 minutes)" ;
058	 
059	var $profile-name = "router-profile";
060	 
061	match / {
062	 
063	    /*
064	     * Get the cpu-usage for the last 60 minutes
065	     */
066	    var $rpc = <get-accounting-record-information> {
067	            <profile> $profile-name;
068	            <since> "-3600";
069	        }
070	 
071	    var $results = jcs:invoke($rpc);
072	 
073	    var $line-raw = {
074	    for-each ($results/routing-engine-accounting-statistics/routing-engine-accounting-record) {
075	 
076	        <xsl:sort select="position()" data-type="number" order="descending">;
077	         
078	        var $tcpu =  round(total-cpu-usage);
079	        var $split = jcs:regex("([0-9])([0-9]?)([0-9]?)", $tcpu);
080	 
081	        if ($tcpu < 10) {
082	            <first-line> "b";
083	        <second-line> "b";
084	        <third-line> $split[2];
085	        } else if ($tcpu < 100) {
086	        <first-line> "b";
087	        <second-line>$split[2];
088	        <third-line> $split[3];
089	        } else {
090	        <first-line> $split[2];
091	        <second-line>$split[3];
092	        <third-line> $split[4];
093	       }
094	 
095	       if ($tcpu > 90 && $tcpu <= 100) {
096	           <y1> "*";
097	       } else {
098	           <y1> "b";
099	       }
100	 
101	       if ($tcpu > 80 and $tcpu <= 90) {
102	           <y2> "*";
103	       } else {
104	           <y2> "b";
105	       }
106	 
107	       if ($tcpu > 70 and $tcpu <= 80) {
108	           <y3> "*";
109	       } else {
110	           <y3> "b";
111	       }
112	 
113	       if ($tcpu > 60 and $tcpu <= 70) {
114	           <y4> "*";
115	       } else {
116	           <y4> "b";
117	       }
118	 
119	       if ($tcpu > 50 and $tcpu <= 60) {
120	           <y5> "*";
121	       } else {
122	           <y5> "b";
123	       }
124	 
125	       if ($tcpu > 40 and $tcpu <= 50) {
126	           <y6> "*";
127	       } else {
128	           <y6> "b";
129	       }
130	 
131	       if ($tcpu > 30 and $tcpu <= 40) {
132	           <y7> "*";
133	       } else {
134	           <y7> "b";
135	       }
136	 
137	       if ($tcpu > 20 and $tcpu <= 30) {
138	           <y8> "*";
139	       } else {
140	           <y8> "b";
141	       }
142	 
143	       if ($tcpu > 10 and $tcpu <= 20) {
144	           <y9> "*";
145	       } else {
146	           <y9> "b";
147	       }
148	       if ($tcpu > 0 and $tcpu <= 10) {
149	           <y10> "*";
150	       } else {
151	           <y10> "b";
152	       }
153	    }
154	    }
155	 
156	    var $lines = ext:node-set($line-raw);
157	 
158	    var $percent-line-out  = {
159	 
160	    expr jcs:printf("%s", "    ");
161	 
162	    for-each ($lines/first-line) {
163	        if (. == "b") {
164	        expr jcs:printf("%s", " ");
165	        } else {
166	        expr jcs:printf("%s", .);
167	        }
168	    }
169	    expr jcs:printf("%s", "\n    ");
170	 
171	    for-each ($lines/second-line) {
172	        if (. == "b") {
173	        expr jcs:printf("%s", " ");
174	        } else {
175	        expr jcs:printf("%s", .);
176	        }
177	    }
178	    expr jcs:printf("%s", "\n    ");
179	 
180	    for-each ($lines/third-line) {
181	        if (. == "b") {
182	        expr jcs:printf("%s", " ");
183	        } else {
184	        expr jcs:printf("%s", .);
185	        }
186	    }
187	    }
188	 
189	    expr jcs:output($percent-line-out);
190	 
191	    var $y1  = {
192	 
193	    expr jcs:printf("%s", "100|");
194	 
195	    for-each ($lines/y1) {
196	        if (. == "b") {
197	        expr jcs:printf("%s", " ");
198	        } else {
199	        expr jcs:printf("%s", .);
200	        }
201	    }
202	    }
203	     
204	    expr jcs:output($y1);
205	 
206	    var $y2  = {
207	 
208	    expr jcs:printf("%s", " 90|");
209	 
210	    for-each ($lines/y2) {
211	        if (. == "b") {
212	        expr jcs:printf("%s", " ");
213	        } else {
214	        expr jcs:printf("%s", .);
215	        }
216	    }
217	    }
218	     
219	    expr jcs:output($y2);
220	 
221	    var $y3  = {
222	 
223	    expr jcs:printf("%s", " 80|");
224	 
225	    for-each ($lines/y3) {
226	        if (. == "b") {
227	        expr jcs:printf("%s", " ");
228	        } else {
229	        expr jcs:printf("%s", .);
230	        }
231	    }
232	    }
233	     
234	    expr jcs:output($y3);
235	 
236	    var $y4  = {
237	 
238	    expr jcs:printf("%s", " 70|");
239	 
240	    for-each ($lines/y4) {
241	        if (. == "b") {
242	        expr jcs:printf("%s", " ");
243	        } else {
244	        expr jcs:printf("%s", .);
245	        }
246	    }
247	    }
248	     
249	    expr jcs:output($y4);
250	 
251	    var $y5  = {
252	 
253	    expr jcs:printf("%s", " 60|");
254	 
255	    for-each ($lines/y5) {
256	        if (. == "b") {
257	        expr jcs:printf("%s", " ");
258	        } else {
259	        expr jcs:printf("%s", .);
260	        }
261	    }
262	    }
263	     
264	    expr jcs:output($y5);
265	 
266	    var $y6  = {
267	 
268	    expr jcs:printf("%s", " 50|");
269	 
270	    for-each ($lines/y6) {
271	        if (. == "b") {
272	        expr jcs:printf("%s", " ");
273	        } else {
274	        expr jcs:printf("%s", .);
275	        }
276	    }
277	    }
278	     
279	    expr jcs:output($y6);
280	 
281	    var $y7  = {
282	 
283	    expr jcs:printf("%s", " 40|");
284	 
285	    for-each ($lines/y7) {
286	        if (. == "b") {
287	        expr jcs:printf("%s", " ");
288	        } else {
289	        expr jcs:printf("%s", .);
290	        }
291	    }
292	    }
293	     
294	    expr jcs:output($y7);
295	 
296	    var $y8  = {
297	 
298	    expr jcs:printf("%s", " 30|");
299	 
300	    for-each ($lines/y8) {
301	        if (. == "b") {
302	        expr jcs:printf("%s", " ");
303	        } else {
304	        expr jcs:printf("%s", .);
305	        }
306	    }
307	    }
308	     
309	    expr jcs:output($y8);
310	 
311	    var $y9  = {
312	 
313	    expr jcs:printf("%s", " 20|");
314	 
315	    for-each ($lines/y9) {
316	        if (. == "b") {
317	        expr jcs:printf("%s", " ");
318	        } else {
319	        expr jcs:printf("%s", .);
320	        }
321	    }
322	    }
323	     
324	    expr jcs:output($y9);
325	 
326	    var $y10  = {
327	 
328	    expr jcs:printf("%s", " 10|");
329	 
330	    for-each ($lines/y10) {
331	        if (. == "b") {
332	        expr jcs:printf("%s", " ");
333	        } else {
334	        expr jcs:printf("%s", .);
335	        }
336	    }
337	    }
338	     
339	    expr jcs:output($y10);
340	 
341	    expr jcs:output($xaxis-line);
342	    expr jcs:output($xaxis-num1);
343	    expr jcs:output($xaxis-num2);
344	    expr jcs:output($xaxis-num3);
345	 
346	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	  <title>cpu-usage-60.slax</title>
04	  <author>phil</author>
05	  <synopsis>
06	    Cool display of CPU usage graphically, similar to Cisco's "show processes cpu history"
07	  </synopsis>
08	  <coe>op</coe>
09	  <type>display</type>
10	 
11	  <description>
12	This op script uses get-accounting-record-information to collect RE CPU usage for the last 60 minutes and display them in graphical format.
13	It can be similarly modified to display router temperature, rtt details, memory usage, etc as well.
14	 
15	  </description>
16	 
17	  <example>
18	    <title>Configuration</title>
19	    <description>Configuration required for this op script</description>
20	    <config>example-1.conf</config>
21	    <output>example-1.output</output>
22	  </example>
23	 
24	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
25	                src="../../../../../web/leaf.js"
26	            type="text/javascript"/>
27	</script>

#How-To
#Slax
#ScriptingHow-To