chrony-network-stats.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #!/bin/bash
  2. set -e
  3. #### Configuration ####
  4. INTERFACE="eth0"
  5. PAGE_TITLE="Network Traffic and Chrony Statistics for ${INTERFACE}"
  6. OUTPUT_DIR="/var/www/chrony-network-stats"
  7. HTML_FILENAME="index.html"
  8. ENABLE_LOGGING="yes"
  9. LOG_FILE="/var/log/chrony-network-stats.log"
  10. RRD_DIR="/var/lib/chrony-rrd"
  11. RRD_FILE="$RRD_DIR/chrony.rrd"
  12. WIDTH=800
  13. HEIGHT=300
  14. TIMEOUT_SECONDS=5
  15. #######################
  16. log_message() {
  17. local level="$1"
  18. local message="$2"
  19. if [[ "$ENABLE_LOGGING" == "yes" ]]; then
  20. echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $message" >> "$LOG_FILE"
  21. fi
  22. echo "[$level] $message"
  23. }
  24. validate_numeric() {
  25. local value="$1"
  26. local name="$2"
  27. if ! [[ "$value" =~ ^[0-9]+$ ]]; then
  28. log_message "ERROR" "Invalid $name: $value. Must be numeric."
  29. exit 1
  30. fi
  31. }
  32. check_commands() {
  33. local commands=("vnstati" "rrdtool" "chronyc" "sudo" "timeout")
  34. for cmd in "${commands[@]}"; do
  35. if ! command -v "$cmd" &>/dev/null; then
  36. log_message "ERROR" "Command '$cmd' not found in PATH."
  37. exit 1
  38. fi
  39. done
  40. }
  41. setup_directories() {
  42. log_message "INFO" "Checking and preparing directories..."
  43. for dir in "$OUTPUT_DIR" "$RRD_DIR" "$OUTPUT_DIR/img"; do
  44. mkdir -p "$dir" || {
  45. log_message "ERROR" "Failed to create directory: $dir"
  46. exit 1
  47. }
  48. if [ ! -w "$dir" ]; then
  49. log_message "ERROR" "Directory '$dir' is not writable."
  50. exit 1
  51. fi
  52. done
  53. }
  54. generate_vnstat_images() {
  55. log_message "INFO" "Generating vnStat images for interface '$INTERFACE'..."
  56. local modes=("s" "d" "t" "h" "m" "y")
  57. for mode in "${modes[@]}"; do
  58. vnstati -"$mode" -i "$INTERFACE" -o "$OUTPUT_DIR/img/vnstat_${mode}.png" || {
  59. log_message "ERROR" "Failed to generate vnstat image for mode $mode Cehck configuaration section : INTERFACE=\"here\""
  60. exit 1
  61. }
  62. done
  63. }
  64. collect_chrony_data() {
  65. log_message "INFO" "Collecting Chrony data..."
  66. get_html() {
  67. timeout "$TIMEOUT_SECONDS"s sudo chronyc "$1" -v 2>&1 | sed 's/&/\&/g;s/</\</g;s/>/\>/g;s/$/<br>/' || {
  68. log_message "ERROR" "Failed to collect chronyc $1 data"
  69. return 1
  70. }
  71. }
  72. RAW_TRACKING=$(timeout "$TIMEOUT_SECONDS"s sudo chronyc tracking) || {
  73. log_message "ERROR" "Failed to collect chronyc tracking data"
  74. exit 1
  75. }
  76. CHRONYC_TRACKING_HTML=$(echo "$RAW_TRACKING" | sed 's/&/\&/g;s/</\</g;s/>/\>/g;s/$/<br>/')
  77. CHRONYC_SOURCES=$(get_html sources) || exit 1
  78. CHRONYC_SOURCESTATS=$(get_html sourcestats) || exit 1
  79. CHRONYC_SELECTDATA=$(get_html selectdata) || exit 1
  80. }
  81. extract_chronyc_values() {
  82. extract_val() {
  83. echo "$RAW_TRACKING" | awk "/$1/ {print \$($2)}" | grep -E '^[-+]?[0-9.]+$' || echo "U"
  84. }
  85. OFFSET=$(extract_val "Last offset" "NF-1")
  86. local systime_line
  87. systime_line=$(echo "$RAW_TRACKING" | grep "System time")
  88. if [[ -n "$systime_line" ]]; then
  89. local value
  90. value=$(echo "$systime_line" | awk '{print $4}')
  91. if [[ "$systime_line" == *"slow"* ]]; then
  92. SYSTIME="-$value"
  93. else
  94. SYSTIME="$value"
  95. fi
  96. else
  97. SYSTIME="U"
  98. fi
  99. FREQ=$(extract_val "Frequency" "NF-2")
  100. RESID_FREQ=$(extract_val "Residual freq" "NF-1")
  101. SKEW=$(extract_val "Skew" "NF-1")
  102. DELAY=$(extract_val "Root delay" "NF-1")
  103. DISPERSION=$(extract_val "Root dispersion" "NF-1")
  104. STRATUM=$(extract_val "Stratum" "3")
  105. RAW_STATS=$(LC_ALL=C sudo chronyc serverstats) || {
  106. log_message "ERROR" "Failed to collect chronyc serverstats"
  107. exit 1
  108. }
  109. get_stat() {
  110. echo "$RAW_STATS" | awk -F'[[:space:]]*:[[:space:]]*' "/$1/ {print \$2}" | grep -E '^[0-9]+$' || echo "U"
  111. }
  112. PKTS_RECV=$(get_stat "NTP packets received")
  113. PKTS_DROP=$(get_stat "NTP packets dropped")
  114. CMD_RECV=$(get_stat "Command packets received")
  115. CMD_DROP=$(get_stat "Command packets dropped")
  116. LOG_DROP=$(get_stat "Client log records dropped")
  117. NTS_KE_ACC=$(get_stat "NTS-KE connections accepted")
  118. NTS_KE_DROP=$(get_stat "NTS-KE connections dropped")
  119. AUTH_PKTS=$(get_stat "Authenticated NTP packets")
  120. INTERLEAVED=$(get_stat "Interleaved NTP packets")
  121. TS_HELD=$(get_stat "NTP timestamps held")
  122. }
  123. create_rrd_database() {
  124. if [ ! -f "$RRD_FILE" ]; then
  125. log_message "INFO" "Creating new RRD file: $RRD_FILE"
  126. LC_ALL=C rrdtool create "$RRD_FILE" --step 300 \
  127. DS:offset:GAUGE:600:U:U DS:frequency:GAUGE:600:U:U DS:resid_freq:GAUGE:600:U:U DS:skew:GAUGE:600:U:U \
  128. DS:delay:GAUGE:600:U:U DS:dispersion:GAUGE:600:U:U DS:stratum:GAUGE:600:0:16 \
  129. DS:systime:GAUGE:600:U:U \
  130. DS:pkts_recv:COUNTER:600:0:U DS:pkts_drop:COUNTER:600:0:U DS:cmd_recv:COUNTER:600:0:U \
  131. DS:cmd_drop:COUNTER:600:0:U DS:log_drop:COUNTER:600:0:U DS:nts_ke_acc:COUNTER:600:0:U \
  132. DS:nts_ke_drop:COUNTER:600:0:U DS:auth_pkts:COUNTER:600:0:U DS:interleaved:COUNTER:600:0:U \
  133. DS:ts_held:GAUGE:600:0:U \
  134. RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:288:730 \
  135. RRA:MAX:0.5:1:576 RRA:MAX:0.5:6:672 RRA:MAX:0.5:24:732 RRA:MAX:0.5:288:730 \
  136. RRA:MIN:0.5:1:576 RRA:MIN:0.5:6:672 RRA:MIN:0.5:24:732 RRA:MIN:0.5:288:730 || {
  137. log_message "ERROR" "Failed to create RRD database"
  138. exit 1
  139. }
  140. fi
  141. }
  142. update_rrd_database() {
  143. log_message "INFO" "Updating RRD database..."
  144. UPDATE_STRING="N:$OFFSET:$FREQ:$RESID_FREQ:$SKEW:$DELAY:$DISPERSION:$STRATUM:$SYSTIME:$PKTS_RECV:$PKTS_DROP:$CMD_RECV:$CMD_DROP:$LOG_DROP:$NTS_KE_ACC:$NTS_KE_DROP:$AUTH_PKTS:$INTERLEAVED:$TS_HELD"
  145. LC_ALL=C rrdtool update "$RRD_FILE" "$UPDATE_STRING" || {
  146. log_message "ERROR" "Failed to update RRD database"
  147. exit 1
  148. }
  149. }
  150. generate_graphs() {
  151. log_message "INFO" "Generating graphs..."
  152. local END_TIME=$(date +%s)
  153. local START_TIME=$((END_TIME - 86400))
  154. declare -A graphs=(
  155. ["chrony_serverstats"]="--title 'Chrony Server Statistics - by day' --vertical-label 'Packets/second' \
  156. --lower-limit 0 --units-exponent 0 \
  157. DEF:pkts_recv='$RRD_FILE':pkts_recv:AVERAGE \
  158. DEF:pkts_drop='$RRD_FILE':pkts_drop:AVERAGE \
  159. DEF:cmd_recv='$RRD_FILE':cmd_recv:AVERAGE \
  160. DEF:cmd_drop='$RRD_FILE':cmd_drop:AVERAGE \
  161. DEF:log_drop='$RRD_FILE':log_drop:AVERAGE \
  162. DEF:nts_ke_acc='$RRD_FILE':nts_ke_acc:AVERAGE \
  163. DEF:nts_ke_drop='$RRD_FILE':nts_ke_drop:AVERAGE \
  164. DEF:auth_pkts='$RRD_FILE':auth_pkts:AVERAGE \
  165. 'COMMENT: \l' \
  166. 'AREA:pkts_recv#C4FFC4:Packets received ' \
  167. 'LINE1:pkts_recv#00E000:' \
  168. 'GPRINT:pkts_recv:LAST:Cur\: %5.2lf%s' \
  169. 'GPRINT:pkts_recv:MIN:Min\: %5.2lf%s' \
  170. 'GPRINT:pkts_recv:AVERAGE:Avg\: %5.2lf%s' \
  171. 'GPRINT:pkts_recv:MAX:Max\: %5.2lf%s\l' \
  172. 'LINE1:pkts_drop#FF8C00:Packets dropped ' \
  173. 'GPRINT:pkts_drop:LAST:Cur\: %5.2lf%s' \
  174. 'GPRINT:pkts_drop:MIN:Min\: %5.2lf%s' \
  175. 'GPRINT:pkts_drop:AVERAGE:Avg\: %5.2lf%s' \
  176. 'GPRINT:pkts_drop:MAX:Max\: %5.2lf%s\l' \
  177. 'LINE1:cmd_recv#4169E1:Command packets received ' \
  178. 'GPRINT:cmd_recv:LAST:Cur\: %5.2lf%s' \
  179. 'GPRINT:cmd_recv:MIN:Min\: %5.2lf%s' \
  180. 'GPRINT:cmd_recv:AVERAGE:Avg\: %5.2lf%s' \
  181. 'GPRINT:cmd_recv:MAX:Max\: %5.2lf%s\l' \
  182. 'LINE1:cmd_drop#FFD700:Command packets dropped ' \
  183. 'GPRINT:cmd_drop:LAST:Cur\: %5.2lf%s' \
  184. 'GPRINT:cmd_drop:MIN:Min\: %5.2lf%s' \
  185. 'GPRINT:cmd_drop:AVERAGE:Avg\: %5.2lf%s' \
  186. 'GPRINT:cmd_drop:MAX:Max\: %5.2lf%s\l' \
  187. 'LINE1:log_drop#9400D3:Client log records dropped ' \
  188. 'GPRINT:log_drop:LAST:Cur\: %5.2lf%s' \
  189. 'GPRINT:log_drop:MIN:Min\: %5.2lf%s' \
  190. 'GPRINT:log_drop:AVERAGE:Avg\: %5.2lf%s' \
  191. 'GPRINT:log_drop:MAX:Max\: %5.2lf%s\l' \
  192. 'LINE1:nts_ke_acc#8A2BE2:NTS-KE connections accepted ' \
  193. 'GPRINT:nts_ke_acc:LAST:Cur\: %5.2lf%s' \
  194. 'GPRINT:nts_ke_acc:MIN:Min\: %5.2lf%s' \
  195. 'GPRINT:nts_ke_acc:AVERAGE:Avg\: %5.2lf%s' \
  196. 'GPRINT:nts_ke_acc:MAX:Max\: %5.2lf%s\l' \
  197. 'LINE1:nts_ke_drop#9370DB:NTS-KE connections dropped ' \
  198. 'GPRINT:nts_ke_drop:LAST:Cur\: %5.2lf%s' \
  199. 'GPRINT:nts_ke_drop:MIN:Min\: %5.2lf%s' \
  200. 'GPRINT:nts_ke_drop:AVERAGE:Avg\: %5.2lf%s' \
  201. 'GPRINT:nts_ke_drop:MAX:Max\: %5.2lf%s\l' \
  202. 'LINE1:auth_pkts#FF0000:Authenticated NTP packets ' \
  203. 'GPRINT:auth_pkts:LAST:Cur\: %5.2lf%s' \
  204. 'GPRINT:auth_pkts:MIN:Min\: %5.2lf%s' \
  205. 'GPRINT:auth_pkts:AVERAGE:Avg\: %5.2lf%s' \
  206. 'GPRINT:auth_pkts:MAX:Max\: %5.2lf%s\l'"
  207. ["chrony_tracking"]="--title 'Chrony Dispersion + Stratum - by day' --vertical-label 'milliseconds' --alt-autoscale \
  208. --units-exponent 0 \
  209. DEF:stratum='$RRD_FILE':stratum:AVERAGE \
  210. DEF:freq='$RRD_FILE':frequency:AVERAGE \
  211. DEF:skew='$RRD_FILE':skew:AVERAGE \
  212. DEF:delay='$RRD_FILE':delay:AVERAGE \
  213. DEF:dispersion='$RRD_FILE':dispersion:AVERAGE \
  214. CDEF:skew_scaled=skew,100,* \
  215. CDEF:delay_scaled=delay,1000,* \
  216. CDEF:disp_scaled=dispersion,1000,* \
  217. 'COMMENT: \l' \
  218. 'LINE1:stratum#00ff00:Stratum ' \
  219. 'GPRINT:stratum:LAST: Cur\: %5.2lf%s' \
  220. 'GPRINT:stratum:MIN:Min\: %5.2lf%s' \
  221. 'GPRINT:stratum:AVERAGE:Avg\: %5.2lf%s' \
  222. 'GPRINT:stratum:MAX:Max\: %5.2lf%s\l' \
  223. 'LINE1:disp_scaled#9400D3:Root dispersion [Root dispersion] ' \
  224. 'GPRINT:disp_scaled:LAST: Cur\: %5.2lf%s' \
  225. 'GPRINT:disp_scaled:MIN:Min\: %5.2lf%s' \
  226. 'GPRINT:disp_scaled:AVERAGE:Avg\: %5.2lf%s' \
  227. 'GPRINT:disp_scaled:MAX:Max\: %5.2lf%s\l'"
  228. ["chrony_offset"]="--title 'Chrony System Time Offset - by day' --vertical-label 'milliseconds' \
  229. DEF:offset='$RRD_FILE':offset:AVERAGE \
  230. DEF:systime='$RRD_FILE':systime:AVERAGE \
  231. CDEF:systime_scaled=systime,1000,* \
  232. CDEF:offset_ms=offset,1000,* \
  233. 'LINE2:offset_ms#00ff00:Actual Offset from NTP Source [Last Offset] ' \
  234. 'GPRINT:offset_ms:LAST: Cur\: %5.2lf%s' \
  235. 'GPRINT:offset_ms:MIN:Min\: %5.2lf%s' \
  236. 'GPRINT:offset_ms:AVERAGE:Avg\: %5.2lf%s' \
  237. 'GPRINT:offset_ms:MAX:Max\: %5.2lf%s\l' \
  238. 'LINE1:systime_scaled#4169E1:System Clock Adjustment [System Time] ' \
  239. 'GPRINT:systime_scaled:LAST: Cur\: %5.2lf%s' \
  240. 'GPRINT:systime_scaled:MIN:Min\: %5.2lf%s' \
  241. 'GPRINT:systime_scaled:AVERAGE:Avg\: %5.2lf%s' \
  242. 'GPRINT:systime_scaled:MAX:Max\: %5.2lf%s\l'"
  243. ["chrony_delay"]="--title 'Chrony Root Delay - by day' --vertical-label 'milliseconds' --units-exponent 0 \
  244. DEF:delay='$RRD_FILE':delay:AVERAGE \
  245. CDEF:delay_ms=delay,1000,* \
  246. LINE2:delay_ms#00ff00:'Network Delay to Root Source [Root Delay] ' \
  247. 'GPRINT:delay_ms:LAST:Cur\: %5.2lf%s' \
  248. 'GPRINT:delay_ms:MIN:Min\: %5.2lf%s' \
  249. 'GPRINT:delay_ms:AVERAGE:Avg\: %5.2lf%s' \
  250. 'GPRINT:delay_ms:MAX:Max\: %5.2lf%s\l'"
  251. ["chrony_frequency"]="--title 'Chrony Clock Frequency Error - by day' --vertical-label 'ppm'\
  252. DEF:freq='$RRD_FILE':frequency:AVERAGE \
  253. DEF:resid_freq='$RRD_FILE':resid_freq:AVERAGE \
  254. CDEF:resfreq_scaled=resid_freq,100,* \
  255. CDEF:freq_scaled=freq,1,* \
  256. 'LINE2:freq_scaled#00ff00:Natural Clock Drift [Frequency] ' \
  257. 'GPRINT:freq_scaled:LAST:Cur\: %5.2lf%s' \
  258. 'GPRINT:freq_scaled:MIN:Min\: %5.2lf%s' \
  259. 'GPRINT:freq_scaled:AVERAGE:Avg\: %5.2lf%s' \
  260. 'GPRINT:freq_scaled:MAX:Max\: %5.2lf%s\n' \
  261. 'LINE1:resfreq_scaled#4169E1:Residual Drift (x100) [Residual freq] ' \
  262. 'GPRINT:resfreq_scaled:LAST:Cur\: %5.2lf%s' \
  263. 'GPRINT:resfreq_scaled:MIN:Min\: %5.2lf%s' \
  264. 'GPRINT:resfreq_scaled:AVERAGE:Avg\: %5.2lf%s' \
  265. 'GPRINT:resfreq_scaled:MAX:Max\: %5.2lf%s\l'"
  266. ["chrony_drift"]="--title 'Chrony Drift Margin Error - by day' --vertical-label 'ppm' \
  267. --units-exponent 0 \
  268. DEF:resid_freq='$RRD_FILE':resid_freq:AVERAGE \
  269. DEF:skew_raw='$RRD_FILE':skew:AVERAGE \
  270. CDEF:resfreq_scaled=resid_freq,100,* \
  271. CDEF:skew_scaled=skew_raw,100,* \
  272. 'COMMENT: \l' \
  273. 'LINE1:skew_scaled#00ff00:Estimate Drift Error Margin (x100) [Skew] ' \
  274. 'GPRINT:skew_scaled:LAST:Cur\: %5.2lf' \
  275. 'GPRINT:skew_scaled:MIN:Min\: %5.2lf' \
  276. 'GPRINT:skew_scaled:AVERAGE:Avg\: %5.2lf' \
  277. 'GPRINT:skew_scaled:MAX:Max\: %5.2lf\l'"
  278. )
  279. for graph in "${!graphs[@]}"; do
  280. local cmd="LC_ALL=C rrdtool graph '$OUTPUT_DIR/img/$graph.png' --width '$WIDTH' --height '$HEIGHT' --start end-1d --end now-180s ${graphs[$graph]}"
  281. eval "$cmd" || {
  282. log_message "ERROR" "Failed to generate graph: $graph"
  283. exit 1
  284. }
  285. done
  286. }
  287. generate_html() {
  288. log_message "INFO" "Generating HTML report..."
  289. local GENERATED_TIMESTAMP=$(date)
  290. cat >"$OUTPUT_DIR/$HTML_FILENAME" <<EOF
  291. <!DOCTYPE html>
  292. <html lang="en">
  293. <head>
  294. <meta charset="utf-8">
  295. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  296. <title>${PAGE_TITLE} - Server Status</title>
  297. <style>
  298. :root {
  299. --primary-text: #212529;
  300. --secondary-text: #6c757d;
  301. --background-color: #f8f9fa;
  302. --content-background: #ffffff;
  303. --border-color: #787879;
  304. --code-background: #e1e1e1;
  305. --code-text: #000000;
  306. }
  307. body {
  308. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  309. margin: 0;
  310. padding: 20px;
  311. background-color: var(--background-color);
  312. color: var(--primary-text);
  313. line-height: 1.6;
  314. }
  315. .container {
  316. max-width: 1400px;
  317. margin: 0 auto;
  318. background-color: var(--content-background);
  319. padding: 20px 20px;
  320. border-radius: 8px;
  321. box-shadow: 0 4px 8px rgba(0,0,0,0.05);
  322. }
  323. header {
  324. text-align: center;
  325. border-bottom: 1px solid var(--border-color);
  326. padding-bottom: 20px;
  327. margin-bottom: 30px;
  328. }
  329. header h1 {
  330. margin: 0;
  331. font-size: 2.5em;
  332. color: var(--primary-text);
  333. }
  334. section {
  335. margin-bottom: 40px;
  336. }
  337. h2 {
  338. font-size: 1.8em;
  339. color: var(--primary-text);
  340. border-bottom: 1px solid var(--border-color);
  341. padding-bottom: 10px;
  342. margin-top: 0;
  343. margin-bottom: 20px;
  344. }
  345. h2 a {
  346. font-size: 0.8em;
  347. font-weight: normal;
  348. vertical-align: middle;
  349. margin-left: 10px;
  350. }
  351. h3 {
  352. font-size: 1.3em;
  353. color: var(--primary-text);
  354. margin-top: 25px;
  355. }
  356. @media (max-width: 767px) {
  357. #vnstat-graphs table,
  358. #vnstat-graphs tbody,
  359. #vnstat-graphs tr,
  360. #vnstat-graphs td {
  361. display: block;
  362. width: 100%;
  363. }
  364. #vnstat-graphs td {
  365. padding-left: 0;
  366. padding-right: 0;
  367. text-align: center;
  368. }
  369. }
  370. .graph-grid {
  371. display: grid;
  372. grid-template-columns: 1fr;
  373. gap: 10px;
  374. text-align: center;
  375. }
  376. @media (min-width: 768px) {
  377. .graph-grid {
  378. grid-template-columns: repeat(2, 1fr);
  379. }
  380. }
  381. figure {
  382. margin: 0;
  383. padding: 0;
  384. }
  385. img {
  386. max-width: 100%;
  387. height: auto;
  388. border: 1px solid var(--border-color);
  389. border-radius: 4px;
  390. box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  391. }
  392. pre {
  393. background-color: var(--code-background);
  394. color: var(--code-text);
  395. padding: 10px;
  396. border: 1px solid #c3bebe;
  397. border-radius: 4px;
  398. overflow-x: auto;
  399. white-space: pre-wrap;
  400. word-wrap: break-word;
  401. font-size: 0.8em;
  402. }
  403. footer {
  404. text-align: center;
  405. margin-top: 40px;
  406. padding-top: 20px;
  407. border-top: 1px solid var(--border-color);
  408. font-size: 0.9em;
  409. color: var(--secondary-text);
  410. }
  411. </style>
  412. </head>
  413. <body>
  414. <div class="container">
  415. <main>
  416. <section id="chrony-graphs">
  417. <h2>Chrony Graphs <a target="_blank" href="https://chrony-project.org/doc/4.3/chronyc.html#:~:text=System%20clock-,tracking,-The%20tracking%20command">[Data Legend]</a></h2>
  418. <div class="graph-grid">
  419. <figure>
  420. <img src="img/chrony_serverstats.png" alt="Chrony server statistics graph">
  421. </figure>
  422. <figure>
  423. <img src="img/chrony_offset.png" alt="Chrony system clock offset graph">
  424. </figure>
  425. <figure>
  426. <img src="img/chrony_tracking.png" alt="Chrony system clock tracking graph">
  427. </figure>
  428. <figure>
  429. <img src="img/chrony_delay.png" alt="Chrony sync delay graph">
  430. </figure>
  431. <figure>
  432. <img src="img/chrony_frequency.png" alt="Chrony clock frequency graph">
  433. </figure>
  434. <figure>
  435. <img src="img/chrony_drift.png" alt="Chrony clock frequency drift graph">
  436. </figure>
  437. </div>
  438. </section>
  439. <section id="vnstat-graphs">
  440. <h2>vnStati Graphs</h2>
  441. <table border="0" style="margin-left: auto; margin-right: auto;">
  442. <tbody>
  443. <tr>
  444. <td valign="top" style="padding: 0 10px;">
  445. <img src="img/vnstat_s.png" alt="vnStat summary"><br>
  446. <img src="img/vnstat_d.png" alt="vnStat daily" style="margin-top: 4px;"><br>
  447. <img src="img/vnstat_t.png" alt="vnStat top 10" style="margin-top: 4px;"><br>
  448. </td>
  449. <td valign="top" style="padding: 0 10px;">
  450. <img src="img/vnstat_h.png" alt="vnStat hourly"><br>
  451. <img src="img/vnstat_m.png" alt="vnStat monthly" style="margin-top: 4px;"><br>
  452. <img src="img/vnstat_y.png" alt="vnStat yearly" style="margin-top: 4px;"><br>
  453. </td>
  454. </tr>
  455. </tbody>
  456. </table>
  457. </section>
  458. <section id="chrony-stats">
  459. <h2>Chrony - NTP Statistics</h2>
  460. <h3>Command: <code>chronyc sources -v</code></h3>
  461. <pre><code>${CHRONYC_SOURCES}</code></pre>
  462. <h3>Command: <code>chronyc selectdata -v</code></h3>
  463. <pre><code>${CHRONYC_SELECTDATA}</code></pre>
  464. <h3>Command: <code>chronyc sourcestats -v</code></h3>
  465. <pre><code>${CHRONYC_SOURCESTATS}</code></pre>
  466. <h3>Command: <code>chronyc tracking</code></h3>
  467. <pre><code>${CHRONYC_TRACKING_HTML}</code></pre>
  468. </section>
  469. </main>
  470. <footer>
  471. <p>Page generated on: ${GENERATED_TIMESTAMP}</p>
  472. </footer>
  473. </div>
  474. </body>
  475. </html>
  476. EOF
  477. }
  478. main() {
  479. log_message "INFO" "Starting vnstati script..."
  480. validate_numeric "$WIDTH" "WIDTH"
  481. validate_numeric "$HEIGHT" "HEIGHT"
  482. validate_numeric "$TIMEOUT_SECONDS" "TIMEOUT_SECONDS"
  483. check_commands
  484. setup_directories
  485. generate_vnstat_images
  486. collect_chrony_data
  487. extract_chronyc_values
  488. create_rrd_database
  489. update_rrd_database
  490. generate_graphs
  491. generate_html
  492. log_message "INFO" "HTML page and graphs generated in: $OUTPUT_DIR/$HTML_FILENAME"
  493. echo "✅ Successfully generated report"
  494. }
  495. main