-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex2_snmp_int_graph.py
108 lines (88 loc) · 3.36 KB
/
ex2_snmp_int_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
'''
Use SNMP v3 to create two image files from pynet-rtr1
'''
from snmp_helper import snmp_get_oid_v3, snmp_extract
import line_graph
import time
from getpass import getpass
def get_interface_stats(snmp_device, snmp_user, stat_type, row_number):
'''
stat_type can be 'in_octets, out_octets, in_ucast_pkts, out_ucast_pkts
returns the counter value as an integer
'''
oid_dict = {
'in_octets': '1.3.6.1.2.1.2.2.1.10',
'out_octets': '1.3.6.1.2.1.2.2.1.16',
'in_ucast_pkts': '1.3.6.1.2.1.2.2.1.11',
'out_ucast_pkts': '1.3.6.1.2.1.2.2.1.17',
}
if not stat_type in oid_dict.keys():
raise ValueError("Invalid value for stat_type: {}" % stat_type)
# Make sure row_number can be converted to integer
row_number = int(row_number)
# Append row number to OID
oid = oid_dict[stat_type]
oid = oid + '.' + str(row_number)
snmp_data = snmp_get_oid_v3(snmp_device, snmp_user, oid)
return int(snmp_extract(snmp_data))
def main():
'''
Connect to router via SNMPv3. Get data and graph it
'''
debug = False
# Get conneciton parameters
#rtr1_ip_addr = raw_input("Enter pynet-rtr1 IP: ")
#my_key = getpass(prompt="Auth + Encryption Key: ")
rtr1_ip_addr = '184.105.247.70'
my_key = '88newclass'
a_user = 'pysnmp'
auth_key = my_key
encrypt_key = my_key
snmp_user = (a_user, auth_key, encrypt_key)
snmp_device = (rtr1_ip_addr, 161)
# Fa4 is in row 5 in the MIB-2 interfaces table
row_number = 5
graph_stats = {
"in_octets": [],
"out_octets": [],
"in_ucast_pkts": [],
"out_ucast_pkts": [],
}
base_count_dict = {}
# Enter a loop gathering SNMP data every 5 minutes for an hour
for time_track in range(0, 65, 5):
print "\n%20s %-60s" % ("time", time_track)
# Gather snmp statistics for these four fields
for entry in ("in_octets", "out_octets", "in_ucast_pkts", "out_ucast_pkts"):
# Get the SNMP data
snmp_retrieved_count = get_interface_stats(snmp_device, snmp_user, entry, row_number)
# Get the base counter value
base_count = base_count_dict.get(entry)
if base_count:
# Save the data to the graph_stats dictionary
graph_stats[entry].append(snmp_retrieved_count - base_count)
print "%20s %-60s" % (entry, graph_stats[entry][-1])
# Update the base counter value
base_count_dict[entry] = snmp_retrieved_count
time.sleep(10)
print
if debug:
print graph_stats
x_labels = []
for x_label in range(5, 65, 5):
x_labels.append(str(x_label))
if debug:
print x_labels
# Create the graphs
if line_graph.twoline("pynet-rtr1-octets.svg", "pynet-rtr1 Fa4 Input/Output Bytes",
graph_stats["in_octets"], "In Octets", graph_stats["out_octets"],
"Out Octets", x_labels):
print "In/Out Octets graph created"
if line_graph.twoline("pynet-rtr1-pkts.svg", "pynet-rtr1 Fa4 Input/Output Unicast Packets",
graph_stats["in_ucast_pkts"], "In Packets", graph_stats["out_ucast_pkts"],
"Out Packets", x_labels):
print "In/Out Packets graph created"
print
if __name__ == '__main__':
main()