Extracting the CPU Utilization Value and Saving It to a Text File

Python 3: Automating Network Tasks Network Application #3 - Extracting Network Parameters & Building Graphs
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.81
List Price:  €129.94
You save:  €37.13
£79.68
List Price:  £111.56
You save:  £31.87
CA$136.70
List Price:  CA$191.38
You save:  CA$54.68
A$151.47
List Price:  A$212.07
You save:  A$60.59
S$134.88
List Price:  S$188.84
You save:  S$53.96
HK$781.18
List Price:  HK$1,093.68
You save:  HK$312.50
CHF 90.49
List Price:  CHF 126.69
You save:  CHF 36.20
NOK kr1,087.35
List Price:  NOK kr1,522.33
You save:  NOK kr434.98
DKK kr692.92
List Price:  DKK kr970.11
You save:  DKK kr277.19
NZ$166.33
List Price:  NZ$232.87
You save:  NZ$66.53
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,943.35
List Price:  ৳15,321.13
You save:  ৳4,377.77
₹8,337.18
List Price:  ₹11,672.39
You save:  ₹3,335.20
RM473.95
List Price:  RM663.55
You save:  RM189.60
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,738.77
List Price:  ₨38,835.40
You save:  ₨11,096.62
฿3,675.50
List Price:  ฿5,145.85
You save:  ฿1,470.34
₺3,235.04
List Price:  ₺4,529.19
You save:  ₺1,294.14
B$509.60
List Price:  B$713.47
You save:  B$203.86
R1,850.72
List Price:  R2,591.08
You save:  R740.36
Лв181.51
List Price:  Лв254.13
You save:  Лв72.61
₩135,590.93
List Price:  ₩189,832.73
You save:  ₩54,241.80
₪370.74
List Price:  ₪519.05
You save:  ₪148.31
₱5,705.78
List Price:  ₱7,988.32
You save:  ₱2,282.54
¥15,303.96
List Price:  ¥21,426.16
You save:  ¥6,122.20
MX$1,697.07
List Price:  MX$2,375.96
You save:  MX$678.89
QR363.69
List Price:  QR509.19
You save:  QR145.49
P1,358.38
List Price:  P1,901.79
You save:  P543.40
KSh13,211.65
List Price:  KSh18,496.84
You save:  KSh5,285.18
E£4,788.95
List Price:  E£6,704.73
You save:  E£1,915.77
ብር5,723.72
List Price:  ብር8,013.44
You save:  ብር2,289.71
Kz83,591.64
List Price:  Kz117,031.64
You save:  Kz33,440
CLP$94,219
List Price:  CLP$131,910.38
You save:  CLP$37,691.37
CN¥723.64
List Price:  CN¥1,013.13
You save:  CN¥289.48
RD$5,794.19
List Price:  RD$8,112.10
You save:  RD$2,317.90
DA13,457.95
List Price:  DA18,841.67
You save:  DA5,383.72
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q775.06
List Price:  Q1,085.12
You save:  Q310.05
GY$20,860.22
List Price:  GY$29,205.14
You save:  GY$8,344.92
ISK kr13,949.49
List Price:  ISK kr19,529.85
You save:  ISK kr5,580.35
DH1,005.73
List Price:  DH1,408.07
You save:  DH402.33
L1,766.28
List Price:  L2,472.87
You save:  L706.58
ден5,712.52
List Price:  ден7,997.75
You save:  ден2,285.23
MOP$802.57
List Price:  MOP$1,123.63
You save:  MOP$321.06
N$1,845.78
List Price:  N$2,584.16
You save:  N$738.38
C$3,670.67
List Price:  C$5,139.09
You save:  C$1,468.41
रु13,313.56
List Price:  रु18,639.52
You save:  रु5,325.95
S/372.22
List Price:  S/521.13
You save:  S/148.90
K385.44
List Price:  K539.63
You save:  K154.19
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,689.66
List Price:  ZK3,765.63
You save:  ZK1,075.97
L462.03
List Price:  L646.86
You save:  L184.83
Kč2,326.92
List Price:  Kč3,257.79
You save:  Kč930.86
Ft36,152.68
List Price:  Ft50,615.20
You save:  Ft14,462.51
SEK kr1,078.64
List Price:  SEK kr1,510.15
You save:  SEK kr431.50
ARS$87,815.44
List Price:  ARS$122,945.14
You save:  ARS$35,129.69
Bs690.51
List Price:  Bs966.74
You save:  Bs276.23
COP$388,367.89
List Price:  COP$543,730.59
You save:  COP$155,362.69
₡50,962.55
List Price:  ₡71,349.61
You save:  ₡20,387.06
L2,463.20
List Price:  L3,448.58
You save:  L985.38
₲746,475.93
List Price:  ₲1,045,096.16
You save:  ₲298,620.23
$U3,821.56
List Price:  $U5,350.33
You save:  $U1,528.77
zł401.98
List Price:  zł562.79
You save:  zł160.80
Already have an account? Log In

Transcript

Okay, now let's get back to the SSH connection.py module, the script that takes care of connecting to the device, executing the commands and returning and saving the output. The goal of our application is to query the switch or router for the value of the CPU utilization once every 10 seconds. In order to obtain this value, we must send the show processes top ones command, as you can see in the CMD dot txt file, and then retrieve the output then search for the value inside that string. So inside the SSH connection function, right after storing the output using the rec v method and the router output variable, we are inserting some new code as you can see right here, that will help us get the CPU utilization value. Once we obtain this output in the form of a string, we can use regular expressions and the search method to extract The value we need.

First, let's head to our Arista switch and issue the same command in order to see how the output looks like on the device. So we have show processes top once. So you can see that inside this rich output, we can easily spot the current value of the CPU utilization, right here where it says percent CPU s, colon, then a couple of spaces, and then this 7.9 value, followed by a space and us and the comma. So this is where we will always find the value that we need. And this is the pattern that we should search for inside the command output. Now we should write some regular expression syntax inside the parentheses of the search method in order to pinpoint our CPU value.

So going back to the code, we have our e dot search, then inside the parentheses, the first argument is the pattern we are looking for. And the second argument after the comma is the variable Pointing to our target string, which in our case is called router output. Now focusing on the first argument of this method, we can see right from the start, this be right here prepending the pattern. This stands for bytes, and it signals the fact that the device doesn't return a raw string, but the bytes like object as it is called in Python, this doesn't change the way we handle patterns and regular expressions. However, Python will throw an error in this case, if you use our for raw string when searching this pattern inside the bytes like object returned by the switch. Next, we have the pattern itself in between double quotes.

So this is the pattern right here. First we have percent CPU, and as in between parentheses and the colon before the value that we actually need to extract. Notice that in this case, I've added a backslash before each of the parentheses surrounding the letter S in order to escape those parentheses. Since the parentheses do have a special meaning in regular expression patterns, they signal a group remember, but in this case, the parentheses are actually part of the output string itself, as you can see right here on the switch, so we don't want them to be interpreted as a group inside our pattern. Next, after the colon, I've used the backslash s special sequence, and a plus sign, as you can see, right here, meaning that between the colon and the value that we need to extract from the switch, there are one or more spaces that we need to consider.

This time, we might consider this sequence of spaces as the first group in the pattern. Next after those spaces, if we take a look at the command output on the switch, we see that we should have the value we are looking for. We can match this value by using a dot meaning any character. So this group right here, so we have a dot meaning any character except a newline character. The plus sign means that the previous expression may Repeat one or more times, so this group will look for at least one character, any character doesn't matter if it is a letter, a number, a space, or a punctuation mark or whatever. The question mark signals the fact that the match is made in a minimal fashion, matching as few characters as possible, until the fourth space is found.

Notice the backslash s special sequence used again, right here, following this group, as suggested by the space character following the CPU value in the output returned by the switch. Finally, we have u s, and the comma, these three characters at the end of our pattern, which are the characters following the CPU utilization value on the switch as well. Now looking at the syntax we have just analyzed, we can see that there are three groups inside this pattern, three pair of parentheses, two of them refer to clusters of space characters, so we don't care about them at all. The only group that is important here is of course, the one matching the CPU utilization value itself. Since in the case of group numbering the counter starts at one not zero as it does for string or list indexing, it means that we want to extract the second group from this pattern.

So, we are creating a new variable called utilization and applying the group method on the CPU variable from up here. Moreover, we need to decode the bytes like object returned by the switch to produce a string and we will use the UTF eight encoding for that, I want to get into a discussion about encoding and decoding since it is outside the scope of this course. Just remember that we can use the decode method to actually convert a byte like object to a string by also specifying the encoding mechanism to be used. Most of the time you will go with the popular UTF eight encoding standard. However, I have attached a link to this lecture for more information about encoding and decoding free, so check that out if you want to learn more. The last thing to add to our SSH connection function is the code that opens up a text file on our local file system and append a new value of the CPU utilization to that file.

I have already created that file. Inside the same folder I used to store all the other scripts and files for this application. And I called it CPU dot txt. So going back to our code, we have with open and inside the parentheses of the open function, we enter the path to the file in between double quotes of course, making sure that we escape each backslash inside the path using another backslash. This way we avoid any issues while searching for the file. Since Python may interpret any backslash n, or backslash T, or backslash capital U inside the path as special characters like the newline character tab or Unicode, escaping all the back slashes in the path is the safest way to go.

Next, remember to open the file for appending. So don't forget to add the a file access mode in between quotes after the comma. So with this file opened as F, we use the right method to append the value referenced by the utilisation variable, and also a newline character at the end to make sure that each value is placed on a separate line inside the text file. Okay, so in conclusion, this piece of code that we've added to our SSH connection function, so this code right here searches for the pattern we defined inside the command output returned by the switch extracts and decodes the CPU utilization value itself, and then append it to the GPU dot txt file each time a query is performed on the device. I'll stop here for now. I'll see you in the next lecture.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.