Zuerst erstellen wir uns in unserer InfluxDB API eine Datenbank.
root:~# influx -precision rfc3339
Connected to http://localhost:8086 version 1.7.9
InfluxDB shell version: 1.7.9
> create database serverstats
<STRG>+<D>
Nun erstellen wir uns unser Skript, welches die entsprechenden Daten ermittelt, um diese uns in unsere InfluxDB Datenbank zu schreiben.
root:~# vi /usr/local/bin/serverstats.sh
Das Skript serverstats.sh
#!/bin/bash
# **********************************************************************
# $File: serverstats.sh $
# $Author: mschulz $ - $Date: 2020-01-27 12:48:09 +0100 (Mon, 27 Jan 2020) $
# $HeadURL: http://svn.tuxnet24.de/operations/bash/serverstats.sh $
# $Description: Script to collect memory/cpu statistics of the current server maschine. $
# **********************************************************************
interval=300 # seconds
file="/tmp/serverstats.dat"
influxapi="http://localhost:8086/write?db=stats"
# Load configuration, when exists
if [ -f /etc/default/serverstats ]; then
. /etc/default/serverstats
[ -n "${INTERVAL}" ] && interval=${INTERVAL}
[ -n "${INFLUXAPI}" ] && influxapi=${INFLUXAPI}
fi
inet_unit="kb"
inet_iface=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')
hostname=$(hostname)
# **********************************************************************
timestamp () {
# Get the current timezone like +2 or -2
timezone=$(date +%z | sed 's/0//g')
# Get only the hours from the timezon like 2
tzshift=$(echo $timezone | sed 's/+\|-//g')
# Set the suffix to ago if timezone is negative
if [[ $timezone == +* ]]; then
suffix=""
else
suffix=" ago"
fi
# Print out the current unixtimestamp not in UTC - in local time
date --date="${tzshift} hour${suffix}" +%s"000000000"
}
# **********************************************************************
stat_traffic () {
local hostname=$1
local timestamp=$2
local iface=$3
local unit=${4:-"b"}
local verbose=${5:-"on"}
local label="Bytes/s"
local prefix="traffic,host=${hostname},type=assembly"
# Get received and transmited bytes of the current interface
local rxold=$(cat /sys/class/net/${iface}/statistics/rx_bytes)
local txold=$(cat /sys/class/net/${iface}/statistics/tx_bytes)
# We wait one second
sleep 1
# Get received and transmited bytes of the current interface
local rxnew=$(cat /sys/class/net/${iface}/statistics/rx_bytes)
local txnew=$(cat /sys/class/net/${iface}/statistics/tx_bytes)
# Calculate received and transmited Byte/KB/MB/ per second of the current interface
if [ $unit = "KB" -o $unit = "kb" ]; then
tx=$(echo "$txnew $txold" | perl -a -n -e 'printf("%.2f", (($F[0]-$F[1])/1024));')
rx=$(echo "$rxnew $rxold" | perl -a -n -e 'printf("%.2f", (($F[0]-$F[1])/1024));')
label="KB/s"
elif [ $unit = "MB" -o $unit = "mb" ]; then
tx=$(echo "$txnew $txold" | perl -a -n -e 'printf("%.2f", (($F[0]-$F[1])/131072));')
rx=$(echo "$rxnew $rxold" | perl -a -n -e 'printf("%.2f", (($F[0]-$F[1])/131072));')
label="MB/s"
else
tx=$(echo "$txnew $txold" | perl -a -n -e 'printf("%.2f", ($F[0]-$F[1]));')
rx=$(echo "$rxnew $rxold" | perl -a -n -e 'printf("%.2f", ($F[0]-$F[1]));')
fi
if [ $verbose = "on" ]; then
local suffix="RX $iface: $rx ${label}, TX $iface: $tx ${label}"
else
local suffix="net_rx=${rx},net_tx=${tx}"
fi
echo "${prefix} ${suffix} ${timestamp}"
}
# **********************************************************************
# Load Average (1,5,15)
stat_loadavg () {
local hostname=$1
local timestamp=$2
local prefix="loadavg,host=${hostname},type=assembly"
local suffix=$(awk '{print "loadavg1="$1",loadavg5="$2",loadavg15="$3}' /proc/loadavg)
echo "${prefix} ${suffix} ${timestamp}"
}
# **********************************************************************
# Physical Memory Usage (total, free)
stat_memoryusage () {
local hostname=$1
local timestamp=$2
local memory=$(awk '$0 ~ /^(MemFree|MemTotal)/{ printf "%%s=%s,", $2 }' /proc/meminfo | sed 's/,$//g')
local prefix="memory,host=${hostname},type=assembly"
local suffix=$(printf "${memory}" "mem_total" "mem_free")
echo "${prefix} ${suffix} ${timestamp}"
}
# **********************************************************************
# Cached Memory Usage (free, buffers, cache)
stat_memorycached () {
local hostname=$1
local timestamp=$2
local memcache=$(awk '$0 ~ /^(MemFree|Buffers|Cached)/{ printf "%%s=%s,", $2 }' /proc/meminfo | sed 's/,$//g')
local prefix="memcache,host=${hostname},type=assembly"
local suffix=$(printf "${memcache}" "mem_free" "mem_buffers" "mem_cache")
echo "${prefix} ${suffix} ${timestamp}"
}
# **********************************************************************
# Swap Usage (total, free)
stat_swapusage () {
local hostname=$1
local timestamp=$2
local swapusage=$(awk '$0 ~ /^(SwapFree|SwapTotal)/{ printf "%%s=%s,", $2 }' /proc/meminfo | sed 's/,$//g')
local prefix="swapusage,host=${hostname},type=assembly"
local suffix=$(printf "${swapusage}" "swap_total" "swap_free")
echo "${prefix} ${suffix} ${timestamp}"
}
# **********************************************************************
# Memory Buffers (total, buffers)
stat_membuffer () {
local hostname=$1
local timestamp=$2
local membuffer=$(awk '$0 ~ /^(Buffers|MemTotal)/{ printf "%%s=%s,", $2 }' /proc/meminfo | sed 's/,$//g')
local prefix="membuffer,host=${hostname},type=assembly"
local suffix=$(printf "${membuffer}" "mem_total" "mem_buffers")
echo "${prefix} ${suffix} ${timestamp}"
}
# **********************************************************************
# Used Space / (total, used)
stat_diskspace () {
local hostname=$1
local timestamp=$2
local suffix=$(df | awk '$6 ~ /^\/$/ {print "space_total="$2",space_used="$3}')
local prefix="usedspace,host=${hostname},type=assembly"
echo "${prefix} ${suffix} ${timestamp}"
}
# **********************************************************************
# CPU Usage (user,nice,system,idle,iowait,irq)
stat_cpuusage () {
local hostname=$1
local timestamp=$2
local suffix=$(head -n1 /proc/stat | awk '{print "user="$2",nice="$3",system="$4",idle="$5",iowait="$6}')
local prefix="cpuusage,host=${hostname},type=assembly"
echo "${prefix} ${suffix} ${timestamp}"
}
# **********************************************************************
# MAIN
trap "rm ${file}" EXIT
while true; do
timestamp=$(timestamp)
(
stat_loadavg ${hostname} ${timestamp}
stat_memoryusage ${hostname} ${timestamp}
stat_memorycached ${hostname} ${timestamp}
stat_swapusage ${hostname} ${timestamp}
stat_membuffer ${hostname} ${timestamp}
stat_cpuusage ${hostname} ${timestamp}
stat_diskspace ${hostname} ${timestamp}
stat_traffic ${hostname} ${timestamp} ${inet_iface} ${inet_unit} "off"
) >>${file}
sleep $interval
curl -i -XPOST ${influxapi} --data-binary @${file} && rm ${file}
done
# vim: syntax=sh ts=4 sw=4 sts=4 sr noet
# EOF
Dann werden wir das Skript noch ausführbar machen.
root:~# chmod 755 /usr/local/bin/serverstats.sh
Nun erstellen wir uns eine Konfigurationsdatei für unseren serverstats.service, welchen wir uns im Anschluß erstellen.
root:~# vi /etc/default/serverstats
Konfigurationsdatei für serverstats.service Unit
#
# This is the configuration file for the Airsonic service
# (/lib/systemd/system/serverstats.service)
#
# Interval in seconds in which the statistics are written/retrieved
INTERVAL=60
# URL to influx API to write the data (http://localhost:8086/write?db=<DB>)
INFLUXAPI="http://localhost:8086/write?db=serverstats"
# vim: syntax=sh ts=4 sw=4 sts=4 sr noet
Dann erstellen wir uns noch die serverstats.service Unit Datei und aktivieren unseren neu erstellten Service.
root:~# vi /lib/systemd/system/serverstats.service
Service Unit Datei serverstats.service
[Unit]
Description=collect memory/cpu statistics of the current server maschine
[Service]
Type=oneshot
ExecStart=/bin/sh -c "/usr/local/bin/serverstats.sh &"
ExecStop=/bin/sh -c "kill $(ps -fade | grep serverstats.sh | grep -v grep | awk '{print $2}')"
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Nun noch den neu erstellten Service aktivieren und starten – fertig 🙂
root:~# systemctl daemon-reload
root:~# systemctl enable serverstats
root:~# systemctl start serverstats