Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This requires that `ifstat`, `sysstat` and `bc` are installed and that `$LIBRATO_USER` and `$LIBRATO_TOKEN` are available in the environment.
#!/bin/bash
hostname=$(hostname)
# Run top twice, first output is cached
top_out=$(top -bn2 -d0.1)
cpu=$(echo "${top_out}" | grep "Cpu(s)" | sed -E "s/.*,\s*([0-9\.]+)\%id.*/\1/" | awk '{print 100 - $1}' | sed -n 2p)
memory_total=$(echo "${top_out}" | grep "Mem:" | awk {'print $2'} | sed s/k// | sed -n 2p)
memory_used=$(echo "${top_out}" | grep "Mem:" | awk {'print $4'} | sed s/k// | sed -n 2p)
memory_percent=$(echo ${memory_used}/${memory_total} | bc -l | cut -c 2-3)
disk=$(df -hl | sed -n 2p | awk {'print $5'} | sed s/%//)
disk_read=$(iostat -dm | awk 'NR > 3' | awk '{s=s+$3}; END{print s}')
disk_write=$(iostat -dm | awk 'NR > 3' | awk '{s=s+$4}; END{print s}')
curl -u ${LIBRATO_USER}:${LIBRATO_TOKEN} \
-d "gauges[0][name]=system.${hostname}.cpu" \
-d "gauges[0][value]=${cpu}" \
-d "gauges[0][source]=${hostname}" \
-d "gauges[1][name]=system.${hostname}.memory" \
-d "gauges[1][value]=${memory_percent}" \
-d "gauges[1][source]=${hostname}" \
-d "gauges[2][name]=system.${hostname}.disk" \
-d "gauges[2][value]=${disk}" \
-d "gauges[2][source]=${hostname}" \
-d "gauges[3][name]=system.${hostname}.disk_io" \
-d "gauges[3][value]=${disk_read}" \
-d "gauges[3][source]=read" \
-d "gauges[4][name]=system.${hostname}.disk_io" \
-d "gauges[4][value]=${disk_write}" \
-d "gauges[4][source]=write" \
-X POST https://metrics-api.librato.com/v1/metrics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.