Created
October 18, 2016 04:46
-
-
Save racingmars/51a1a681c1e0c1d2c34803fee02461c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# RxRate (set "units" in Grafana to bits per second, the *8 converts bytes to bits): | |
SELECT mean("rxrate") * 8 FROM "network_traffic" WHERE "host" = 'gw' AND "interface" = 'eth0' AND $timeFilter GROUP BY time($interval) | |
# TxRate (set "units" in Grafana to bits per second, the *8 converts bytes to bits): | |
SELECT mean("txrate") * 8 FROM "network_traffic" WHERE "host" = 'gw' AND "interface" = 'eth0' AND $timeFilter GROUP BY time($interval) | |
# 30-day Rx data (I use this as a "singlestat" dashboard item) | |
SELECT sum("rx") FROM "autogen"."historical_network_traffic" WHERE "host" = 'gw' AND "interface" = 'eth0' AND time >= now() - 30d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script get the current bandwidth usage (rate and total bytes) | |
# | |
# Derived from: | |
# https://git.denlab.io/dencur/grafana_scripts_public/blob/master/inetmon.sh | |
sleeptime=5 | |
router=192.168.44.1 | |
community=public | |
interface=2 | |
hostname=gw | |
ifname=eth0 | |
influx=localhost:8086 | |
dbname=home | |
#We need to get a baseline for the traffic before starting the loop | |
#otherwise we have nothing to base out calculations on. | |
#Get in and out octets | |
oldin=`snmpget -v 2c -c $community $router IF-MIB::ifInOctets.$interface -Ov` | |
oldout=`snmpget -v 2c -c $community $router IF-MIB::ifOutOctets.$interface -Ov` | |
#Strip out the value from the string | |
oldin=$(echo $oldin | cut -c 12-) | |
oldout=$(echo $oldout | cut -c 12-) | |
#Prepare to start the loop and warn the user | |
echo "Press [CTRL+C] to stop..." | |
while : | |
do | |
#We need to wait between readings to have something to compare to | |
sleep "$sleeptime" | |
#Get in and out octets | |
in=`snmpget -v 2c -c $community $router IF-MIB::ifInOctets.$interface -Ov` | |
out=`snmpget -v 2c -c $community $router IF-MIB::ifOutOctets.$interface -Ov` | |
#Strip out the value from the string | |
in=$(echo $in | cut -c 12-) | |
out=$(echo $out | cut -c 12-) | |
#Get the difference between the old and current | |
diffin=$((in - oldin)) | |
diffout=$((out - oldout)) | |
#Calculate the bytes-per-second | |
inbps=$((diffin / sleeptime )) | |
outbps=$((diffout / sleeptime )) | |
#Seems we need some basic data validation - can't have values less than 0! | |
if [[ $inbps -lt 0 || $outbps -lt 0 ]]; | |
then | |
#There is an issue with one or more readings, get fresh ones | |
#then wait for the next loop to calculate again. | |
echo "We have a problem...moving to plan B" | |
#Get in and out octets | |
oldin=`snmpget -v 2c -c $community $router IF-MIB::ifInOctets.$interface -Ov` | |
oldout=`snmpget -v 2c -c $community $router IF-MIB::ifOutOctets.$interface -Ov` | |
#Strip out the value from the string | |
oldin=$(echo $oldin | cut -c 12-) | |
oldout=$(echo $oldout | cut -c 12-) | |
else | |
#Output the current traffic | |
echo "Main current inbound traffic: $inbps Bps" | |
echo "Main current outbound traffic: $outbps Bps" | |
#Write the data to the database, on 1-second boundaries | |
ts=`date -u +%s` | |
ts=$((ts * 1000000000)) | |
echo "Timestamp: $ts" | |
curl -i -XPOST "http://${influx}/write?db=$dbname" --data-binary "network_traffic,host=$hostname,interface=$ifname rx=$diffin,tx=$diffout,rxrate=$inbps,txrate=$outbps $ts" | |
#Move the current variables to the old ones | |
oldin=$in | |
oldout=$out | |
fi | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make sure a "home" database is created, or whatever $dbname in the inetmon.sh script is set to | |
CREATE DATABASE home | |
# The collection script will write to network_traffic table. | |
# Create a 30-day retention policy in the "home" database and make it the default | |
CREATE RETENTION POLICY 30d ON home DURATION 30d REPLICATION 1 DEFAULT | |
# Create a continuous query to downsample the 5-second data into 5-minute data in | |
# the "autogen" (forever) retention policy in a table named cq_network_traffic | |
CREATE CONTINUOUS QUERY cq_network_traffic ON home | |
BEGIN | |
SELECT sum(rx) AS rx, mean(rxrate) AS rxrate, sum(tx) AS tx, mean(txrate) AS txrate | |
INTO home.autogen.historical_network_traffic | |
FROM home."30d".network_traffic | |
GROUP BY time(5m), * | |
END | |
# After running the collection script in a terminal to make sure it's working and there are no errors, I run it as: | |
$ ./inetmon.sh > /dev/null 2>&1 & | |
(Then run 'disown' so you can log out without killing the process... I'll get it running with | |
init scripts someday...actually I'll write a little collection agent in Go someday to handle | |
counter rollover properly so there isn't a small discrepancy in the data and run that as a | |
service.) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment