json in et out ok
This commit is contained in:
parent
278e3bb39d
commit
87b52d81fa
@ -1,6 +1,12 @@
|
||||
ip=192.168.0.100
|
||||
community=comcacti
|
||||
version=2c
|
||||
ifnames=(Gi1/0/22 Gi1/0/25 Gi1/0/27 Gi1/0/1 Gi1/0/2)
|
||||
# refaire en JSON
|
||||
# parser avec jq : https://stedolan.github.io/jq/
|
||||
{
|
||||
"ip": "192.168.0.100",
|
||||
"community": "comcacti",
|
||||
"version": "2c",
|
||||
"interfaces": {
|
||||
"Gi1/0/22": "truc",
|
||||
"Gi1/0/25": "bidule",
|
||||
"Gi1/0/27": "machin",
|
||||
"Gi1/0/1": null,
|
||||
"Gi1/0/2": null
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
# Récupération des volumes de données passées par les interfaces
|
||||
# dépendances : snmp, jq
|
||||
# dépendances : bash4+, snmp, jq
|
||||
|
||||
# Tableau associatif principal pour stocker uniquement les interfaces à monitorer de l'équipement :
|
||||
# interfaces[nom] = index incrémental applicatif
|
||||
@ -20,10 +20,13 @@
|
||||
snmpwalkPath=/usr/bin/snmpwalk
|
||||
# path de snmpget sur le système
|
||||
snmpgetPath=/usr/bin/snmpget
|
||||
# fichier de log du script
|
||||
ficLog=bandwith.log
|
||||
# path de jq sur le système
|
||||
jqPath=/usr/bin/jq
|
||||
|
||||
# fichier de log du script (nom très différent pour ne pas écraser involontairement les autres fichiers)
|
||||
ficLog=log_bw.log
|
||||
# fichier des resultats des mesures
|
||||
ficMes=bandwith.json
|
||||
ficMes=bandwidth_results.json
|
||||
|
||||
# délai d'attente avant la prochaine mesure en secondes
|
||||
delay=1
|
||||
@ -54,8 +57,16 @@ if [ ! -e $1 ]; then
|
||||
echo 'Erreur : fichier '$1' non trouvé.'
|
||||
exit 1
|
||||
fi;
|
||||
source $1;
|
||||
|
||||
# recupération des variables de la configuration
|
||||
ip=$($jqPath -r .ip $1)
|
||||
community=$($jqPath -r .community $1)
|
||||
version=$($jqPath -r .version $1)
|
||||
listeNoms=$($jqPath -r '.interfaces | keys[]' $1)
|
||||
ifnames=()
|
||||
for n in $listeNoms; do
|
||||
ifnames+=($n)
|
||||
done
|
||||
|
||||
# Index des interfaces de l'équipement : tableau associatif, exemple : interfaces["Gi1/0/6"] = 3
|
||||
declare -A interfaces
|
||||
@ -111,7 +122,7 @@ for w in $walkIfname; do
|
||||
ifMonitor[$index]=$i
|
||||
|
||||
# log de l'entrée monitorée
|
||||
echo $i monitorée >> $ficLog
|
||||
echo $i monitorée : $(date '+%Y-%m-%d %H:%M:%S') >> $ficLog
|
||||
|
||||
# plus besoin de continuer la boucle si on a trouvé l'interface
|
||||
break
|
||||
@ -144,8 +155,10 @@ do
|
||||
ifId=${ifIds[$index]}
|
||||
# récupération du nom
|
||||
nom=${ifMonitor[$index]}
|
||||
# collecte de la description
|
||||
resDescription=$($snmpgetPath $getParams $oidIfDescriptions.$ifId)
|
||||
# collecte de la description et suppression des doubles quotes éventuelles
|
||||
temp=$($snmpgetPath $getParams $oidIfDescriptions.$ifId)
|
||||
temp="${temp%\"}"
|
||||
resDescription="${temp#\"}"
|
||||
# collecte de la vitesse
|
||||
resSpeed=$($snmpgetPath $getParams $oidIfSpeeds.$ifId)
|
||||
# collecte des octets IN
|
||||
@ -153,11 +166,17 @@ do
|
||||
# collecte des octets OUT
|
||||
resOUT=$($snmpgetPath $getParams $oidIfBytesOUT.$ifId)
|
||||
|
||||
echo $nom $resDescription $resSpeed $resIN $resOUT $(date +%s)>> $ficMes
|
||||
tail -n $nbLignes $ficMes > tmp
|
||||
cat tmp > $ficMes
|
||||
# écriture du résultat brut dans un fichier et taille du fichier à $nbLignes
|
||||
echo $nom $resDescription $resSpeed $resIN $resOUT $(date +%s)>> res.raw
|
||||
tail -n $nbLignes res.raw > tmp
|
||||
cat tmp > res.raw
|
||||
|
||||
done
|
||||
# formatage du résultat JSON
|
||||
# http://support.gnip.com/articles/data-and-rule-management-with-jq.html
|
||||
jq '. | split("\n") | map( split(" ") | {ifname: .[0], description: .[1], speed: .[2], in: .[3], out: .[4], ts: .[5]} ) ' -R -s res.raw > $ficMes
|
||||
|
||||
# attente avant prochaine mesure
|
||||
sleep $delay
|
||||
done
|
||||
|
||||
|
@ -0,0 +1,130 @@
|
||||
[
|
||||
{
|
||||
"ifname": "Gi1/0/1",
|
||||
"description": "GigabitEthernet1/0/1",
|
||||
"speed": "1000000000",
|
||||
"in": "2067878572",
|
||||
"out": "1222676877",
|
||||
"ts": "1544798858"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/2",
|
||||
"description": "GigabitEthernet1/0/2",
|
||||
"speed": "1000000000",
|
||||
"in": "3503039885",
|
||||
"out": "2629740670",
|
||||
"ts": "1544798858"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/22",
|
||||
"description": "GigabitEthernet1/0/22",
|
||||
"speed": "1000000000",
|
||||
"in": "190148388",
|
||||
"out": "1779481055",
|
||||
"ts": "1544798858"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/1",
|
||||
"description": "GigabitEthernet1/0/1",
|
||||
"speed": "1000000000",
|
||||
"in": "2068439056",
|
||||
"out": "1223252707",
|
||||
"ts": "1544798859"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/2",
|
||||
"description": "GigabitEthernet1/0/2",
|
||||
"speed": "1000000000",
|
||||
"in": "3503118826",
|
||||
"out": "2629820651",
|
||||
"ts": "1544798859"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/22",
|
||||
"description": "GigabitEthernet1/0/22",
|
||||
"speed": "1000000000",
|
||||
"in": "190811666",
|
||||
"out": "1779647045",
|
||||
"ts": "1544798859"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/1",
|
||||
"description": "GigabitEthernet1/0/1",
|
||||
"speed": "1000000000",
|
||||
"in": "2068581445",
|
||||
"out": "1223595489",
|
||||
"ts": "1544798860"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/2",
|
||||
"description": "GigabitEthernet1/0/2",
|
||||
"speed": "1000000000",
|
||||
"in": "3503251304",
|
||||
"out": "2629921244",
|
||||
"ts": "1544798860"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/22",
|
||||
"description": "GigabitEthernet1/0/22",
|
||||
"speed": "1000000000",
|
||||
"in": "191509100",
|
||||
"out": "1779780390",
|
||||
"ts": "1544798860"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/1",
|
||||
"description": "GigabitEthernet1/0/1",
|
||||
"speed": "1000000000",
|
||||
"in": "2068764940",
|
||||
"out": "1223890938",
|
||||
"ts": "1544798861"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/2",
|
||||
"description": "GigabitEthernet1/0/2",
|
||||
"speed": "1000000000",
|
||||
"in": "3503381088",
|
||||
"out": "2630045308",
|
||||
"ts": "1544798861"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/22",
|
||||
"description": "GigabitEthernet1/0/22",
|
||||
"speed": "1000000000",
|
||||
"in": "192447700",
|
||||
"out": "1780077141",
|
||||
"ts": "1544798861"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/1",
|
||||
"description": "GigabitEthernet1/0/1",
|
||||
"speed": "1000000000",
|
||||
"in": "2068913083",
|
||||
"out": "1224157082",
|
||||
"ts": "1544798862"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/2",
|
||||
"description": "GigabitEthernet1/0/2",
|
||||
"speed": "1000000000",
|
||||
"in": "3503459959",
|
||||
"out": "2630139970",
|
||||
"ts": "1544798862"
|
||||
},
|
||||
{
|
||||
"ifname": "Gi1/0/22",
|
||||
"description": "GigabitEthernet1/0/22",
|
||||
"speed": "1000000000",
|
||||
"in": "192913137",
|
||||
"out": "1780181248",
|
||||
"ts": "1544798862"
|
||||
},
|
||||
{
|
||||
"ifname": null,
|
||||
"description": null,
|
||||
"speed": null,
|
||||
"in": null,
|
||||
"out": null,
|
||||
"ts": null
|
||||
}
|
||||
]
|
45
log_bw.log
Normal file
45
log_bw.log
Normal file
@ -0,0 +1,45 @@
|
||||
Gi1/0/1 monitorée : 2018-12-14 14:56:55
|
||||
Gi1/0/2 monitorée : 2018-12-14 14:56:55
|
||||
Gi1/0/22 monitorée : 2018-12-14 14:56:55
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:20:22
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:20:22
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:20:22
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:20:47
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:20:47
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:20:47
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:21:32
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:21:32
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:21:32
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:21:49
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:21:49
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:21:49
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:22:02
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:22:02
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:22:02
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:23:54
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:23:54
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:23:54
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:25:11
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:25:11
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:25:11
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:26:01
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:26:01
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:26:01
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:26:54
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:26:54
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:26:54
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:27:16
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:27:16
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:27:16
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:33:56
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:33:56
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:33:56
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:38:59
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:38:59
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:38:59
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:46:46
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:46:46
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:46:46
|
||||
Gi1/0/1 monitorée : 2018-12-14 15:47:38
|
||||
Gi1/0/2 monitorée : 2018-12-14 15:47:38
|
||||
Gi1/0/22 monitorée : 2018-12-14 15:47:38
|
Loading…
x
Reference in New Issue
Block a user