Auto check share memory utilization for IOX by Python

Introduction

In some scenario, we need to monitor some data in router/switch by automation. This article will show example that how to check share memory utilization. And you can easy to change the script base on your requirement/scenario.

Prepare

Due to “telnetlib” couldn’t exactly check expect messages by read_until() function (that couldn’t control exactly time when the info return to buffer), so I change to “expect”. And follow Bo’s example Python Expect Demo, and there is a good documents for expect demo from IBM too: 探索 Pexpect,第 2 部分:Pexpect 的实例分析

You can install the expect by follow steps:

wget http://pexpect.sourceforge.net/pexpect-2.3.tar.gz
tar -xzvf pexpect-2.3.tar.gz
cd pexpect-2.3
python setup install (require root)

Solution

Back to our example, at sometime, we maybe match share memory issue, and couldn’t do anything when issue happened… we want to check which process take so many memory, so need to check that by automation, as follow cli info, want to check totally “Usage(KB)“:

RP/0/RSP0/CPU0:ASR9001-B#show shmwin summary
Fri Aug 25 03:24:24.008 UTC
----------------------------------------
Shared memory window summary information
----------------------------------------
Virtual Memory size  : 1024 MBytes
Virtual Memory Range :  0x9c000000 - 0xdc000000
Virtual Memory Group 2 size  : 352 MBytes
Virtual Memory Group 2 Range : 0x66000000 - 0x7c000000

Window Name      ID  GRP #Usrs #Wrtrs Ownr Usage(KB) Peak(KB) Peak Timestamp
---------------- --- --- ----- ------ ---- --------- -------- -------------------
soasync-app-1    34  1   2     2      1054 2         0        --/--/---- --:--:--
soasync-6        43  1   1     1      0    2         0        --/--/---- --:--:--
soasync-5        41  1   1     1      0    2         0        --/--/---- --:--:--
soasync-4        39  1   1     1      0    2         0        --/--/---- --:--:--
soasync-3        37  1   1     1      0    2         0        --/--/---- --:--:--
......

So we can use follow python to achieve that:

#-*- encoding:UTF-8 -*-

import pexpect
import getpass,os
import re

##########  variable  ##########

host = [
'10.75.49.29',
'10.75.49.30',
'10.75.49.29'
];

flag = "===================================================="
print ">>> please put the username/pass for asr9k"
username = raw_input('Hostname: ')
password = getpass.getpass()
# avoid password echo by function getpass

##########  main function  ##########

for x in range(0, len(host)):
    conn = pexpect.spawn("telnet "+host[x])
    index1 = conn.expect(["Username", "telnet: connect to address", pexpect.EOF, pexpect.TIMEOUT])
    if (index1 != 0):
        print flag
        print ">>> hostname " + host[x] + ". login failured due to no route or other issue!"
        conn.close(force=True)
        continue
    conn.sendline(username)
    index2 = conn.expect(["Password: ", pexpect.EOF, pexpect.TIMEOUT], timeout=1)
    if (index2 != 0):
        print flag
        print ">>> hostname " + host[x] + ", empty username or other issue!"
        conn.close(force=True)
        continue
    conn.sendline(password)
    index3 = conn.expect(["#", pexpect.EOF, pexpect.TIMEOUT], timeout=1)
    if (index3 != 0):
        print flag
        print ">>> hostname " + host[x] + ". User/Pass not correct!"
        conn.close(force=True)
        continue
    hostname = re.split(':', conn.before)[1]
    print flag
    print ">>> hostname: " + hostname
    conn.sendline("term len 0")
    conn.expect("#")
    conn.sendline("show shmwin summary")
    conn.expect(hostname)
    shmwin = conn.before
    conn.sendline("exit")

    lines = shmwin.split("\n")
    tvms = int(lines[5].split()[4])
    # data in rows 5 and columns 4, please notices, list counter from "0"
    print ">>> totaly share memory: " + str(tvms)
    total = 0
    for i in range(12, len(lines)-1):
        used = int(lines[i].split()[6])
        total += used
    print ">>> share memory ultilization:" + str(total/tvms)

    f = open('frank.txt', 'a')
    f.write(flag + '\n')
    f.write(">>> " + hostname + '\n')
    f.write(flag + '\n')
    f.write(shmwin + '\n')
    f.close()

Follow console test result, I simulate 4 scenario:

  • Empty username
  • Authentication failed
  • No route
  • Login success
[root@frank ~]# python check-mem-1.py
>>> please put the username/pass for asr9k
Hostname:
Password:
====================================================
>>> hostname 10.75.49.29, empty username or other issue!
====================================================
>>> hostname 10.75.49.30. login failured due to no route or other issue!
====================================================
>>> hostname 10.75.49.29, empty username or other issue!
[root@frank ~]#
[root@frank ~]# python check-mem-1.py
>>> please put the username/pass for asr9k
Hostname: cisco
Password:
====================================================
>>> hostname 10.75.49.29. User/Pass not correct!
====================================================
>>> hostname 10.75.49.30. login failured due to no route or other issue!
====================================================
>>> hostname 10.75.49.29. User/Pass not correct!
[root@frank ~]#
[root@frank ~]# python check-mem-1.py
>>> please put the username/pass for asr9k
Hostname: cisco
Password:
====================================================
>>> hostname: ASR9001-B
>>> totaly share memory: 1024
>>> share memory ultilization:37
====================================================
>>> hostname 10.75.49.30. login failured due to no route or other issue!
====================================================
>>> hostname: ASR9001-B
>>> totaly share memory: 1024
>>> share memory ultilization:37

After exec the script, all show info will auto backup to “frank.txt”, as follow:

[root@frank ~]# more  frank.txt
====================================================
>>> ASR9001-B
====================================================
show shmwin summary
Fri Aug 25 03:54:56.892 UTC
----------------------------------------
Shared memory window summary information
----------------------------------------
Virtual Memory size  : 1024 MBytes
Virtual Memory Range :  0x9c000000 - 0xdc000000
Virtual Memory Group 2 size  : 352 MBytes
Virtual Memory Group 2 Range : 0x66000000 - 0x7c000000

Window Name      ID  GRP #Usrs #Wrtrs Ownr Usage(KB) Peak(KB) Peak Timestamp
---------------- --- --- ----- ------ ---- --------- -------- -------------------
soasync-app-1    34  1   2     2      1054 2         0        --/--/---- --:--:--
soasync-6        43  1   1     1      0    2         0        --/--/---- --:--:--
soasync-5        41  1   1     1      0    2         0        --/--/---- --:--:--
soasync-4        39  1   1     1      0    2         0        --/--/---- --:--:--
soasync-3        37  1   1     1      0    2         0        --/--/---- --:--:--
soasync-2        35  1   1     1      0    2         0        --/--/---- --:--:--
soasync-1        33  1   2     2      442  2         0        --/--/---- --:--:--
ipsla_ot         51  1   2     1      280  7         7        08/09/2017 23:14:39
vkg_l2vpn_bport  73  1   1     1      1186 1547      1547     08/09/2017 23:14:25
......
本文出自 Frank's Blog

版权声明:


本文链接:Auto check share memory utilization for IOX by Python
版权声明:本文为原创文章,仅代表个人观点,版权归 Frank Zhao 所有,转载时请注明本文出处及文章链接
你可以留言,或者trackback 从你的网站

留言哦

blonde teen swallows load.xxx videos