Skip to content

Instantly share code, notes, and snippets.

@ham1
Last active November 10, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ham1/9ab960c02f6d8ff2d9981dc639cb0c46 to your computer and use it in GitHub Desktop.
Save ham1/9ab960c02f6d8ff2d9981dc639cb0c46 to your computer and use it in GitHub Desktop.
Using Bash (vmstat, awk and curl) we can send Linux machine metrics
#!/bin/bash
# Defensive bash script settings
set -o nounset -o pipefail -o errexit -o verbose -o noglob
# Runs vmstat every 1 second and sends results to InfluxDB
vmstat 1 | awk -vhost_name=$HOSTNAME '/ [0-9]/ {
timestamp=systime()"000000000"
tags="host="host_name
print "r,"tags" value="$1,timestamp
print "b,"tags" value="$2,timestamp
print "swpd,"tags" value="$3,timestamp
print "free,"tags" value="$4,timestamp
print "buff,"tags" value="$5,timestamp
print "cache,"tags" value="$6,timestamp
print "si,"tags" value="$7,timestamp
print "so,"tags" value="$8,timestamp
print "bi,"tags" value="$9,timestamp
print "bo,"tags" value="$10,timestamp
print "intr,"tags" value="$11,timestamp
print "cs,"tags" value="$12,timestamp
print "us,"tags" value="$13,timestamp
print "sy,"tags" value="$14,timestamp
print "id,"tags" value="$15,timestamp
print "wa,"tags" value="$16,timestamp
print "st,"tags" value="$17,timestamp
}
' | \
split --lines 17 \
--filter='\
curl -i \
-XPOST "http://influxdb/write?db=dbname" \
--data-binary @-'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment