Skip to content

Instantly share code, notes, and snippets.

@mattrude
Last active February 20, 2024 18:44
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 mattrude/a5b91fd88ec35969af121bd9b3b58a91 to your computer and use it in GitHub Desktop.
Save mattrude/a5b91fd88ec35969af121bd9b3b58a91 to your computer and use it in GitHub Desktop.
Chrony Time Service Status Report

Chrony Status Report

To install, run the following as the pi user

curl -Ls "https://gist.github.com/mattrude/a5b91fd88ec35969af121bd9b3b58a91/raw/reporter-installer.sh" |bash

After the install is complete, confirm the script will run by following:

systemctl list-timers --all

Look for the below entry.

NEXT                        LEFT          LAST                        PASSED        UNIT                         ACTIVATES
Tue 2024-02-20 12:30:00 CST 9min left     Tue 2024-02-20 12:15:00 CST 5min ago      chrony-reporter.timer        chrony-reporter.service
[Unit]
Description=statistics reporter for Chrony NTP service
Requisite=chrony.service
[Service]
Type=simple
User=root
IOSchedulingClass=idle
ExecStart=/usr/local/bin/chrony-reporter.sh
#!/bin/bash
Help() {
echo "Add description of the script functions here."
echo
echo "Syntax: chrony-status.sh [-c|f|h|p]"
echo "options:"
echo "c Add the config file to the output."
echo "f <file> The name & location of the output file."
echo "h Print this Help."
echo "s Do not Print the report to the terminal."
echo
}
ARCHIVE="false"
CONFIG="false"
PRINT="false"
WEB="true"
WEBAN="false"
while getopts ":achswWf:" flag
do
case "${flag}" in
a) ARCHIVE="true";;
c) CONFIG="true";;
h) Help
exit;;
s) PRINT="false";;
w) WEB="true";;
W) WEBAN="true";;
f) FILE=${OPTARG};;
\?) echo "Error: Invalid Option!"
Help
exit;;
esac
done
if [ -z ${FILE} ]; then
FILE=/var/lib/chrony-status/time.txt
fi
if [ ! -d /var/lib/chrony-status ]; then
echo "Creating /var/lib/chrony-status"
sudo mkdir -p /var/lib/chrony-status
sudo chown pi:www-data /var/lib/chrony-status
fi
echo "The Odin Network Time Service on `cat /etc/hostname` (`hostname -I |awk '{print $1}'`)" > $FILE
echo -n "Current Time : " >> $FILE
echo `date` >> $FILE
echo -n "Load Average : " >> $FILE
cat /proc/loadavg |awk '{ print $1", "$2", " $3 }' >> $FILE
echo -n "System Uptime : " >> $FILE
uptime -p |sed 's/up //g' >> $FILE
echo -n "Process Uptime : " >> $FILE
ps -p `sudo cat /var/run/chrony/chronyd.pid` -o etime= |sed 's/ //g' >> $FILE
echo -n "CPU Temperature : " >> $FILE
/usr/bin/vcgencmd measure_temp | awk -F "[=']" '{print($2 * 1.8)+32}' >> $FILE
echo "" >> $FILE
echo "Server Tracking" >> $FILE
chronyc tracking >> $FILE
echo -n "Software : " >> $FILE
chronyc --version |awk '{ print $4" "$2 }' >> $FILE
echo "" >> $FILE
echo "Server Clients" >> $FILE
sudo chronyc clients |awk '!/^localhost/' |awk '!/^127.0.0.1/' |grep -v time.*local >> $FILE
echo "" >> $FILE
echo "Server Sources" >> $FILE
sudo chronyc -N sources >> $FILE
echo "" >> $FILE
echo "Server Sources Stats" >> $FILE
sudo chronyc -N sourcestats >> $FILE
echo "" >> $FILE
echo "Server Sources Auth" >> $FILE
sudo chronyc -N authdata >> $FILE
echo "" >> $FILE
if [ ${WEB} == "true" ]; then
cp $FILE /var/www/html/
fi
if [ ${CONFIG} == "true" ]; then
echo "Server Config file (/etc/chrony/chrony.conf)" >> $FILE
echo "==============================================================================" >> $FILE
cat /etc/chrony/chrony.conf |sed "/^#/d" |sed '/^$/d' >> $FILE
fi
if [ ${ARCHIVE} == "true" ]; then
if [ -f /var/lib/chrony-status/status-`date +%Y%m`.db ]; then
echo "insert into logs values('`date`','`cat /var/www/html/time.txt`');" |sqlite3 /var/lib/chrony-status/status-`date +%Y%m`.db
else
echo "create table logs (datetime text, log text);" |sqlite3 /var/lib/chrony-status/status-`date +%Y%m`.db
echo "insert into logs values('`date`','`cat /var/www/html/time.txt`');" |sqlite3 /var/lib/chrony-status/status-`date +%Y%m`.db
fi
fi
if [ ${WEBAN} == "true" ]; then
sudo cp $FILE /var/www/html/
fi
if [ ${PRINT} == "true" ]; then
cat ${FILE}
fi
[Unit]
Description=Run Chrony Reporter
[Timer]
OnCalendar=*:0/15:0
Persistent=true
AccuracySec=1s
[Install]
WantedBy=timers.target
#/bin/bash
sudo curl -Ls https://gist.github.com/mattrude/a5b91fd88ec35969af121bd9b3b58a91/raw/chrony-reporter.sh -o /usr/local/bin/chrony-reporter.sh
sudo curl -Ls https://gist.github.com/mattrude/a5b91fd88ec35969af121bd9b3b58a91/raw/chrony-reporter.timer -o /usr/lib/systemd/system/chrony-reporter.timer
sudo curl -Ls https://gist.github.com/mattrude/a5b91fd88ec35969af121bd9b3b58a91/raw/chrony-reporter.service -o /usr/lib/systemd/system/chrony-reporter.service
sudo chmod 750 /usr/local/bin/chrony-reporter.sh
sudo systemctl enable chrony-reporter.timer
sudo systemctl start chrony-reporter.timer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment