-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_usage.sh
60 lines (57 loc) · 3.04 KB
/
monitor_usage.sh
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
#!/bin/bash
# SETTINGS
# Policy Passphrase
POLICY_PASS='secret123'
# Authentication protocol passphrase
AUTH_PASS='secret123'
# SNMPv3 username
SNMP_USER='user1'
# List of host to check
CHECK_HOST=("coresw1" "sw1" "sw2")
# Host portmap files. Specifie the name of each file.
HOST_PORTMAP=("/scripts/portmap_coresw1" "/scripts/portmap_sw1" "/scripts/portmap_sw2")
# Generate table of interface mapped to OID.
function GenIntTable() {
interface=`snmpwalk -v 3 -a SHA -A $AUTH_PASS -x AES -X $POLICY_PASS -u $SNMP_USER -l Authpriv $currentHost iso.3.6.1.2.1.2.2.1.2 -P e 2> /dev/null`
interface=`echo "$interface" | cut -d'.' -f 11 | sed 's/STRING://g' | tr -s ' '`
echo "$interface" >> portmap_$currentHost
}
# Get average usage ingress traffic on ports
function AvgPortIn() {
usageIn=`snmpwalk -v 3 -a SHA -A $AUTH_PASS -x AES -X $POLICY_PASS -u $SNMP_USER -l Authpriv $currentHost STATISTICS-MIB::hpSwitchPortStatAvgInPortUtil -P e 2> /dev/null`
usageIn=`echo "$usageIn" | sed 's/STATISTICS-MIB::hpSwitchPortStatAvgInPortUtil.//g' | sed 's/INTEGER://g' | tr -s ' '`
echo "$usageIn"
}
# Get average usage outgress traffic on ports
function AvgPortOut() {
usageOut=`snmpwalk -v 3 -a SHA -A $AUTH_PASS -x AES -X $POLICY_PASS -u $SNMP_USER -l Authpriv $currentHost STATISTICS-MIB::hpSwitchPortStatAvgOutPortUtil -P e 2> /dev/null`
usageOut=`echo "$usageOut" | sed 's/STATISTICS-MIB::hpSwitchPortStatAvgOutPortUtil.//g' | sed 's/INTEGER://g' | tr -s ' '`
echo "$usageOut"
}
function AvgPort() {
usageIn=`snmpwalk -v 3 -a SHA -A $AUTH_PASS -x AES -X $POLICY_PASS -u $SNMP_USER -l Authpriv $currentHost STATISTICS-MIB::hpSwitchPortStatAvgInPortUtil -P e 2> /dev/null`
usageIn=`echo "$usageIn" | sed 's/STATISTICS-MIB::hpSwitchPortStatAvgInPortUtil.//g' | sed 's/INTEGER://g' | tr -s ' '`
usageOut=`snmpwalk -v 3 -a SHA -A $AUTH_PASS -x AES -X $POLICY_PASS -u $SNMP_USER -l Authpriv $currentHost STATISTICS-MIB::hpSwitchPortStatAvgOutPortUtil -P e 2> /dev/null`
usageOut=`echo "$usageOut" | sed 's/STATISTICS-MIB::hpSwitchPortStatAvgOutPortUtil.//g' | sed 's/INTEGER://g' | tr -s ' '`
echo "$usageIn"
echo "$usageOut"
}
# Map port and returned value
function Map() {
while IFS= read -r avgIn; do
avgInID=`echo "$avgIn" | cut -d' ' -f1` # Port ID
avgIn=`echo "$avgIn" | cut -d' ' -f3` # Port usage ingress
avgOut=`echo -e "$usageAvgOut" | grep ^"${avgInID} =" | cut -d' ' -f3`
port=`cat $currentPortmap | grep ^"${avgInID} =" | cut -d '"' -f2` # Interface
mapped=`echo "$port;$avgIn;$avgOut"`
influx -host 127.0.0.1 -username admin -password 'secret' -database switchPortUse -execute "INSERT portUse,port=$port switch=\"$currentHost\",in=$avgIn,out=$avgOut"
done < <(printf '%s\n' "$usageAvg")
}
for i in "${!CHECK_HOST[@]}"; do
currentPortmap=`echo ${HOST_PORTMAP[$i]}`
currentHost=`echo ${CHECK_HOST[$i]}`
usageAvg=`AvgPortIn` # output: portID = int (61 = 11)
usageAvgOut=`AvgPortOut`
Map
done
#GenIntTable