-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhetrixtools_agent.sh
More file actions
769 lines (687 loc) · 28.4 KB
/
hetrixtools_agent.sh
File metadata and controls
769 lines (687 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#!/bin/bash
#
#
# HetrixTools Server Monitoring Agent - macOS
# Copyright 2015 - 2026 @ HetrixTools
# For support, please open a ticket on our website https://hetrixtools.com
#
#
# DISCLAIMER OF WARRANTY
#
# The Software is provided "AS IS" and "WITH ALL FAULTS," without warranty of any kind,
# including without limitation the warranties of merchantability, fitness for a particular purpose and non-infringement.
# HetrixTools makes no warranty that the Software is free of defects or is suitable for any particular purpose.
# In no event shall HetrixTools be responsible for loss or damages arising from the installation or use of the Software,
# including but not limited to any indirect, punitive, special, incidental or consequential damages of any character including,
# without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses.
# The entire risk as to the quality and performance of the Software is borne by you, the user.
#
# END OF DISCLAIMER OF WARRANTY
# Set PATH/Locale
export LC_NUMERIC="C"
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/homebrew/bin:/opt/homebrew/sbin
ScriptPath=$(dirname "${BASH_SOURCE[0]}")
# Agent Version (do not change)
Version="2.0.0"
# Load configuration file
if [ -f "$ScriptPath"/hetrixtools.cfg ]
then
. "$ScriptPath"/hetrixtools.cfg
else
echo "Error: Configuration file not found at $ScriptPath/hetrixtools.cfg"
exit 1
fi
# Script start time
ScriptStartTime=$(date +[%Y-%m-%d\ %T)
##############################################################################
# Helper: key-value store using temp files (bash 3.2 has no associative arrays)
##############################################################################
KV_DIR=$(mktemp -d /tmp/hetrixtools_kv.XXXXXX)
trap "rm -rf '$KV_DIR'" EXIT
kv_set() { # usage: kv_set namespace key value
local ns="$1" key="$2" val="$3"
mkdir -p "$KV_DIR/$ns"
printf '%s' "$val" > "$KV_DIR/$ns/$key"
}
kv_get() { # usage: kv_get namespace key [default]
local ns="$1" key="$2" default="${3:-0}"
if [ -f "$KV_DIR/$ns/$key" ]; then
cat "$KV_DIR/$ns/$key"
else
echo "$default"
fi
}
# Service status function
servicestatus() {
if (( $(ps -ef | grep -E "[\/\ ]$1([^\/]|$)" | grep -v "grep" | wc -l) > 0 ))
then
echo "1"
else
if launchctl list 2>/dev/null | grep -qi "$1"
then
echo "1"
else
echo "0"
fi
fi
}
# Function used to prepare base64 str for url encoding
base64prep() {
str=$1
str="${str//+/%2B}"
str="${str//\//%2F}"
echo "$str"
}
# Function used to perform outgoing PING tests
pingstatus() {
local TargetName=$1
local PingTarget=$2
if ! echo "$TargetName" | grep -qE '^[A-Za-z0-9._-]+$'; then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Invalid PING target name value" >> "$ScriptPath"/debug.log; fi
exit 1
fi
if ! echo "$PingTarget" | grep -qE '^[A-Za-z0-9.:_-]+$'; then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Invalid PING target value" >> "$ScriptPath"/debug.log; fi
exit 1
fi
PING_OUTPUT=$(ping "$PingTarget" -c "$OutgoingPingsCount" 2>/dev/null)
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T])PING_OUTPUT:\n$PING_OUTPUT" >> "$ScriptPath"/debug.log; fi
PACKET_LOSS=$(echo "$PING_OUTPUT" | grep -o '[0-9.]*% packet loss' | cut -d'%' -f1)
if [ -z "$PACKET_LOSS" ]; then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Unable to extract packet loss" >> "$ScriptPath"/debug.log; fi
exit 1
fi
RTT_LINE=$(echo "$PING_OUTPUT" | grep 'round-trip min/avg/max')
if [ -n "$RTT_LINE" ]; then
AVG_RTT=$(echo "$RTT_LINE" | awk -F'/' '{print $5}')
AVG_RTT=$(echo | awk "{print $AVG_RTT * 1000}" | awk '{printf "%18.0f",$1}' | xargs)
else
AVG_RTT="0"
fi
echo "$TargetName,$PingTarget,$PACKET_LOSS,$AVG_RTT;" >> "$ScriptPath"/ping.txt
}
# Check if the agent needs to run Outgoing PING tests
if [ "$1" == "ping" ]
then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Starting PING: $2 ($3) $OutgoingPingsCount times" >> "$ScriptPath"/debug.log; fi
pingstatus "$2" "$3"
exit 1
fi
# Clear debug.log every day at midnight
if [ -z "$(date +%H | sed 's/^0*//')" ] && [ -z "$(date +%M | sed 's/^0*//')" ] && [ -f "$ScriptPath"/debug.log ]
then
rm -f "$ScriptPath"/debug.log
fi
# Start timers
START=$(date +%s)
tTIMEDIFF=0
# Get current minute
M=$(date +%M | sed 's/^0*//')
if [ -z "$M" ]; then
M=0
if [ -f "$ScriptPath"/hetrixtools_cron.log ]; then
rm -f "$ScriptPath"/hetrixtools_cron.log
fi
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Starting HetrixTools Agent v$Version (macOS)" >> "$ScriptPath"/debug.log; fi
# Kill any lingering agent processes
HTProcesses=$(pgrep -f hetrixtools_agent.sh | wc -l | xargs)
if [ -z "$HTProcesses" ]; then HTProcesses=0; fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Found $HTProcesses agent processes" >> "$ScriptPath"/debug.log; fi
if [ "$HTProcesses" -ge 50 ]; then
pgrep -f hetrixtools_agent.sh | xargs kill -9
fi
if [ "$HTProcesses" -ge 10 ]; then
for PID in $(pgrep -f hetrixtools_agent.sh); do
PID_TIME=$(ps -p "$PID" -oetime= 2>/dev/null | tr '-' ':' | awk -F: '{total=0; m=1;} {for (i=0; i < NF; i++) {total += $(NF-i)*m; m *= i >= 2 ? 24 : 60 }} {print total}')
if [ -n "$PID_TIME" ] && [ "$PID_TIME" -ge 90 ]; then
kill -9 "$PID" 2>/dev/null
fi
done
fi
# Outgoing PING (background)
if [ -n "$OutgoingPings" ]; then
OLD_IFS="$IFS"
IFS='|'
for i in $OutgoingPings; do
TargetName=${i%%,*}
TargetIP=${i#*,}
bash "$ScriptPath"/hetrixtools_agent.sh ping "$TargetName" "$TargetIP" &
done
IFS="$OLD_IFS"
fi
# Network interfaces
if [ -n "$NetworkInterfaces" ]; then
OLD_IFS="$IFS"; IFS=','; NetworkInterfacesArray=($NetworkInterfaces); IFS="$OLD_IFS"
else
NetworkInterfacesArray=()
for iface in $(networksetup -listallhardwareports 2>/dev/null | grep "^Device:" | awk '{print $2}'); do
if ifconfig "$iface" 2>/dev/null | grep -q "status: active"; then
NetworkInterfacesArray+=("$iface")
fi
done
# Fallback
if [ ${#NetworkInterfacesArray[@]} -eq 0 ]; then
for iface in $(ifconfig -lu 2>/dev/null | tr ' ' '\n' | grep -E '^en[0-9]+$'); do
if ifconfig "$iface" 2>/dev/null | grep -q "inet "; then
NetworkInterfacesArray+=("$iface")
fi
done
fi
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Network Interfaces: ${NetworkInterfacesArray[*]}" >> "$ScriptPath"/debug.log; fi
# Initial network usage
for NIC in "${NetworkInterfacesArray[@]}"; do
NETSTAT_LINE=$(netstat -ibI "$NIC" 2>/dev/null | grep -w "$NIC" | grep -v "Link#" | head -1)
if [ -z "$NETSTAT_LINE" ]; then
NETSTAT_LINE=$(netstat -ibI "$NIC" 2>/dev/null | tail -1)
fi
init_rx=$(echo "$NETSTAT_LINE" | awk '{print $7}')
init_tx=$(echo "$NETSTAT_LINE" | awk '{print $10}')
kv_set "aRX" "$NIC" "${init_rx:-0}"
kv_set "aTX" "$NIC" "${init_tx:-0}"
kv_set "tRX" "$NIC" "0"
kv_set "tTX" "$NIC" "0"
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Network Interface $NIC RX: ${init_rx:-0} TX: ${init_tx:-0}" >> "$ScriptPath"/debug.log; fi
done
# Auto-detect listening ports
if [ -z "${ConnectionPorts// }" ]; then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Detecting external connection ports" >> "$ScriptPath"/debug.log; fi
AutoDetectedPorts=$(lsof -iTCP -sTCP:LISTEN -nP 2>/dev/null | awk 'NR>1 {print $9}' | grep -oE '[0-9]+$' | sort -n | uniq | head -30 | tr '\n' ',' | sed 's/,$//')
if [ -n "$AutoDetectedPorts" ]; then
ConnectionPorts="$AutoDetectedPorts"
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Auto detected connection ports: $ConnectionPorts" >> "$ScriptPath"/debug.log; fi
fi
fi
# Port connections init
ConnectionPortsArray=()
if [ -n "$ConnectionPorts" ]; then
OLD_IFS="$IFS"; IFS=','; ConnectionPortsArray=($ConnectionPorts); IFS="$OLD_IFS"
for cPort in "${ConnectionPortsArray[@]}"; do
kv_set "conn" "$cPort" "0"
done
fi
# Check Services (initial)
CheckServicesArray=()
if [ -n "$CheckServices" ]; then
OLD_IFS="$IFS"; IFS=','; CheckServicesArray=($CheckServices); IFS="$OLD_IFS"
for svc in "${CheckServicesArray[@]}"; do
val=$(servicestatus "$svc")
kv_set "srvcs" "$svc" "$val"
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Service $svc status: $val" >> "$ScriptPath"/debug.log; fi
done
fi
# Calculate how many data sample loops
RunTimes=$(echo | awk "{print int(60 / $CollectEveryXSeconds)}")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Collecting data for $RunTimes loops" >> "$ScriptPath"/debug.log; fi
# Initialize totals
tCPU=0
tCPUus=0
tCPUsy=0
tRAM=0
tRAMSwap=0
tloadavg1=0
tloadavg5=0
tloadavg15=0
# Get total physical RAM in bytes
TOTAL_RAM_BYTES=$(sysctl -n hw.memsize 2>/dev/null)
PAGE_SIZE=$(sysctl -n hw.pagesize 2>/dev/null)
if [ -z "$PAGE_SIZE" ] || [ "$PAGE_SIZE" -eq 0 ] 2>/dev/null; then PAGE_SIZE=16384; fi
# Initial disk IOPS snapshot (per-disk via ioreg)
# Parse all IOBlockStorageDriver entries to get per-physical-disk read/write bytes
IOPS_DISK_LIST=""
IOPS_TIME_START=$(date +%s)
ioreg -c IOBlockStorageDriver -r -l -d 3 2>/dev/null | awk '
/IOBlockStorageDriver/{stats_r=""; stats_w=""; bsd=""}
/"Statistics"/{
r=$0; sub(/.*"Bytes \(Read\)"=/, "", r); sub(/[,}].*/, "", r); stats_r=r
w=$0; sub(/.*"Bytes \(Write\)"=/, "", w); sub(/[,}].*/, "", w); stats_w=w
}
/"BSD Name" = "disk[0-9]+"/{
b=$0; sub(/.*"BSD Name" = "/, "", b); sub(/".*/, "", b); bsd=b
if(bsd != "" && stats_r != "") print bsd ":" stats_r ":" stats_w
}
' | while IFS=: read disk rd wr; do
kv_set "iops_r" "$disk" "${rd:-0}"
kv_set "iops_w" "$disk" "${wr:-0}"
echo "$disk"
done > "$KV_DIR/iops_disklist"
IOPS_DISK_LIST=$(cat "$KV_DIR/iops_disklist" 2>/dev/null | tr '\n' ' ')
# Build physical-disk-to-mount mapping via diskutil list
# Finds synthesized APFS containers and maps them back to physical disks
# Format stored: kv_set "iops_mnt" <physical_disk> <mount_point>
# Default mount for a physical disk is "/"
for disk in $IOPS_DISK_LIST; do
kv_set "iops_mnt" "$disk" "/"
done
# Parse df mounts and trace each back to its physical disk
df -l 2>/dev/null | awk 'NR>1 && /\/dev\/disk/{print $1, $NF}' > "$KV_DIR/df_mounts"
while read -r dev mnt; do
# Extract base disk identifier (e.g., disk3 from /dev/disk3s1s1)
base=$(echo "$dev" | sed 's|/dev/||; s/s[0-9].*//')
# Check if it's a synthesized APFS container by looking for Physical Store
phys=$(diskutil info "$base" 2>/dev/null | grep "Physical Store" | awk '{print $NF}' | sed 's/s[0-9].*//')
if [ -z "$phys" ]; then
phys="$base"
fi
# Only map the root mount "/" or /Volumes/* mounts (skip system volumes)
case "$mnt" in
/|/Volumes/*)
kv_set "iops_mnt" "$phys" "$mnt"
;;
esac
done < "$KV_DIR/df_mounts"
if [ "$DEBUG" -eq 1 ]; then
for disk in $IOPS_DISK_LIST; do
dr=$(kv_get "iops_r" "$disk" 0)
dw=$(kv_get "iops_w" "$disk" 0)
dm=$(kv_get "iops_mnt" "$disk" "/")
echo -e "$ScriptStartTime-$(date +%T]) IOPS start: $disk ($dm) Read=$dr Write=$dw" >> "$ScriptPath"/debug.log
done
fi
# Collect data loop
X=0
for i in $(seq "$RunTimes"); do
X=$((X + 1))
# CPU usage via top
TOP_OUTPUT=$(top -l 2 -n 0 -s "$CollectEveryXSeconds" 2>/dev/null | grep "CPU usage" | tail -1)
CPU_USER=$(echo "$TOP_OUTPUT" | awk -F'[:,]' '{print $2}' | grep -oE '[0-9]+\.[0-9]+')
CPU_SYS=$(echo "$TOP_OUTPUT" | awk -F'[:,]' '{print $3}' | grep -oE '[0-9]+\.[0-9]+')
CPU_IDLE=$(echo "$TOP_OUTPUT" | awk -F'[:,]' '{print $4}' | grep -oE '[0-9]+\.[0-9]+')
if [ -n "$CPU_IDLE" ]; then
CPU=$(echo | awk "{print 100 - $CPU_IDLE}")
else
CPU=0
fi
tCPU=$(echo | awk "{print $tCPU + $CPU}")
tCPUus=$(echo | awk "{print $tCPUus + ${CPU_USER:-0}}")
tCPUsy=$(echo | awk "{print $tCPUsy + ${CPU_SYS:-0}}")
# CPU Load averages
loadavg=$(sysctl -n vm.loadavg 2>/dev/null | tr -d '{}' | xargs)
la1=$(echo "$loadavg" | awk '{print $1}')
la5=$(echo "$loadavg" | awk '{print $2}')
la15=$(echo "$loadavg" | awk '{print $3}')
tloadavg1=$(echo | awk "{print $tloadavg1 + ${la1:-0}}")
tloadavg5=$(echo | awk "{print $tloadavg5 + ${la5:-0}}")
tloadavg15=$(echo | awk "{print $tloadavg15 + ${la15:-0}}")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) CPU: $CPU User: ${CPU_USER:-0} Sys: ${CPU_SYS:-0} Load: ${la1:-0} ${la5:-0} ${la15:-0}" >> "$ScriptPath"/debug.log; fi
# RAM usage via vm_stat
VMSTAT_OUTPUT=$(vm_stat 2>/dev/null)
PAGES_ACTIVE=$(echo "$VMSTAT_OUTPUT" | grep "Pages active:" | awk '{print $3}' | tr -d '.')
PAGES_WIRED=$(echo "$VMSTAT_OUTPUT" | grep "Pages wired down:" | awk '{print $4}' | tr -d '.')
PAGES_COMPRESSED=$(echo "$VMSTAT_OUTPUT" | grep "Pages occupied by compressor:" | awk '{print $5}' | tr -d '.')
PAGES_ACTIVE=${PAGES_ACTIVE:-0}
PAGES_WIRED=${PAGES_WIRED:-0}
PAGES_COMPRESSED=${PAGES_COMPRESSED:-0}
USED_PAGES=$((PAGES_ACTIVE + PAGES_WIRED + PAGES_COMPRESSED))
TOTAL_PAGES=$((TOTAL_RAM_BYTES / PAGE_SIZE))
if [ "$TOTAL_PAGES" -gt 0 ]; then
RAM=$(echo | awk "{print $USED_PAGES * 100 / $TOTAL_PAGES}")
else
RAM=0
fi
tRAM=$(echo | awk "{print $tRAM + $RAM}")
# Swap usage
SWAP_INFO=$(sysctl -n vm.swapusage 2>/dev/null)
SWAP_TOTAL=$(echo "$SWAP_INFO" | grep -oE 'total = [0-9.]+[A-Z]' | grep -oE '[0-9.]+')
SWAP_USED=$(echo "$SWAP_INFO" | grep -oE 'used = [0-9.]+[A-Z]' | grep -oE '[0-9.]+')
if [ -n "$SWAP_TOTAL" ] && [ "$(echo "$SWAP_TOTAL" | awk '{print ($1 > 0)}')" = "1" ]; then
RAMSwap=$(echo | awk "{print ${SWAP_USED:-0} * 100 / $SWAP_TOTAL}")
else
RAMSwap=0
fi
tRAMSwap=$(echo | awk "{print $tRAMSwap + $RAMSwap}")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) RAM: $RAM Swap: $RAMSwap" >> "$ScriptPath"/debug.log; fi
# Network usage
END=$(date +%s)
TIMEDIFF=$((END - START))
if [ "$TIMEDIFF" -le 0 ]; then TIMEDIFF=1; fi
tTIMEDIFF=$((tTIMEDIFF + TIMEDIFF))
START=$END
for NIC in "${NetworkInterfacesArray[@]}"; do
NETSTAT_LINE=$(netstat -ibI "$NIC" 2>/dev/null | grep -w "$NIC" | grep -v "Link#" | head -1)
if [ -z "$NETSTAT_LINE" ]; then
NETSTAT_LINE=$(netstat -ibI "$NIC" 2>/dev/null | tail -1)
fi
CURR_RX=$(echo "$NETSTAT_LINE" | awk '{print $7}')
CURR_TX=$(echo "$NETSTAT_LINE" | awk '{print $10}')
CURR_RX=${CURR_RX:-0}
CURR_TX=${CURR_TX:-0}
PREV_RX=$(kv_get "aRX" "$NIC" 0)
PREV_TX=$(kv_get "aTX" "$NIC" 0)
PREV_TRX=$(kv_get "tRX" "$NIC" 0)
PREV_TTX=$(kv_get "tTX" "$NIC" 0)
RX=$(echo | awk "{print ($CURR_RX - $PREV_RX) / $TIMEDIFF}" | awk '{printf "%18.0f",$1}' | xargs)
TX=$(echo | awk "{print ($CURR_TX - $PREV_TX) / $TIMEDIFF}" | awk '{printf "%18.0f",$1}' | xargs)
kv_set "aRX" "$NIC" "$CURR_RX"
kv_set "aTX" "$NIC" "$CURR_TX"
kv_set "tRX" "$NIC" "$(echo | awk "{print $PREV_TRX + $RX}" | awk '{printf "%18.0f",$1}' | xargs)"
kv_set "tTX" "$NIC" "$(echo | awk "{print $PREV_TTX + $TX}" | awk '{printf "%18.0f",$1}' | xargs)"
done
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Network loop $X done" >> "$ScriptPath"/debug.log; fi
# Port connections
if [ ${#ConnectionPortsArray[@]} -gt 0 ]; then
for cPort in "${ConnectionPortsArray[@]}"; do
CONN_COUNT=$(lsof -iTCP:"$cPort" -sTCP:ESTABLISHED -nP 2>/dev/null | grep -v "^COMMAND" | wc -l | xargs)
prev=$(kv_get "conn" "$cPort" 0)
kv_set "conn" "$cPort" "$(echo | awk "{print $prev + $CONN_COUNT}")"
done
fi
# Check if minute changed
MM=$(date +%M | sed 's/^0*//')
if [ -z "$MM" ]; then MM=0; fi
if [ "$MM" -ne "$M" ]; then
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Minute changed, ending loop" >> "$ScriptPath"/debug.log; fi
break
fi
done
# Get user running the agent
User=$(whoami)
# Check if system requires reboot
RequiresReboot=0
# Operating System
OS_NAME=$(sw_vers -productName 2>/dev/null)
OS_VER=$(sw_vers -productVersion 2>/dev/null)
OS=$(echo -ne "$OS_NAME $OS_VER" | base64 | tr -d '\n\r\t ')
# Kernel
Kernel=$(uname -r | base64 | tr -d '\n\r\t ')
# Hostname
Hostname=$(uname -n | base64 | tr -d '\n\r\t ')
# Uptime
BOOT_TIME=$(sysctl -n kern.boottime 2>/dev/null | awk -F'sec = ' '{print $2}' | awk -F',' '{print $1}')
CURRENT_TIME=$(date +%s)
if [ -n "$BOOT_TIME" ]; then
Uptime=$((CURRENT_TIME - BOOT_TIME))
else
Uptime=0
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) User: $User Hostname: $(echo "$Hostname" | base64 -d 2>/dev/null) Uptime: $Uptime" >> "$ScriptPath"/debug.log; fi
# CPU model
CPUModel=$(sysctl -n machdep.cpu.brand_string 2>/dev/null)
if [ -z "$CPUModel" ]; then
CPUModel=$(sysctl -n hw.model 2>/dev/null)
fi
CPUModel=$(echo -ne "$CPUModel" | base64 | tr -d '\n\r\t ')
# CPU info
CPUSockets=$(sysctl -n hw.packages 2>/dev/null || echo "1")
CPUCores=$(sysctl -n hw.physicalcpu 2>/dev/null || echo "1")
CPUThreads=$(sysctl -n hw.logicalcpu 2>/dev/null || echo "1")
# CPU clock speed (MHz)
CPUSpeed=$(sysctl -n hw.cpufrequency 2>/dev/null)
if [ -n "$CPUSpeed" ] && [ "$CPUSpeed" -gt 0 ] 2>/dev/null; then
CPUSpeed=$((CPUSpeed / 1000000))
else
# Try system_profiler for Intel Macs
CPUSpeed=$(system_profiler SPHardwareDataType 2>/dev/null | grep -i "Processor Speed" | head -1 | grep -oE '[0-9.]+\s*GHz' | grep -oE '[0-9.]+' | awk '{printf "%d", $1 * 1000}')
# Apple Silicon: get max P-cluster frequency from powermetrics
if [ -z "$CPUSpeed" ] || [ "$CPUSpeed" -eq 0 ] 2>/dev/null; then
if command -v powermetrics > /dev/null 2>&1; then
CPUSpeed=$(powermetrics --samplers cpu_power -i 1 -n 1 2>/dev/null | grep "P-Cluster HW active residency" | grep -oE '[0-9]+ MHz' | tail -1 | grep -oE '[0-9]+')
fi
fi
if [ -z "$CPUSpeed" ]; then CPUSpeed=0; fi
fi
# Averages
CPU=$(echo | awk "{print $tCPU / $X}")
CPUus=$(echo | awk "{print $tCPUus / $X}")
CPUsy=$(echo | awk "{print $tCPUsy / $X}")
CPUwa=0
CPUst=0
loadavg1=$(echo | awk "{print $tloadavg1 / $X}")
loadavg5=$(echo | awk "{print $tloadavg5 / $X}")
loadavg15=$(echo | awk "{print $tloadavg15 / $X}")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) CPU: $CPU Cores: $CPUCores Threads: $CPUThreads Speed: $CPUSpeed" >> "$ScriptPath"/debug.log; fi
# RAM size (in KB)
RAMSize=$((TOTAL_RAM_BYTES / 1024))
RAM=$(echo | awk "{print $tRAM / $X}")
# Swap
RAMSwapSize_raw=$(sysctl -n vm.swapusage 2>/dev/null | grep -oE 'total = [0-9.]+[A-Z]')
SWAP_NUM=$(echo "$RAMSwapSize_raw" | grep -oE '[0-9.]+')
SWAP_UNIT=$(echo "$RAMSwapSize_raw" | grep -oE '[A-Z]$')
case "$SWAP_UNIT" in
G) RAMSwapSize=$(echo "$SWAP_NUM" | awk '{printf "%.0f", $1 * 1024 * 1024}') ;;
M) RAMSwapSize=$(echo "$SWAP_NUM" | awk '{printf "%.0f", $1 * 1024}') ;;
K) RAMSwapSize=$(echo "$SWAP_NUM" | awk '{printf "%.0f", $1}') ;;
*) RAMSwapSize=0 ;;
esac
if [ "$RAMSwapSize" -gt 0 ] 2>/dev/null; then
RAMSwap=$(echo | awk "{print $tRAMSwap / $X}")
else
RAMSwap=0
fi
RAMBuff=0
RAMCache=0
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) RAM Size: $RAMSize Usage: $RAM Swap Size: $RAMSwapSize Swap: $RAMSwap" >> "$ScriptPath"/debug.log; fi
# Disks usage
DISKs=""
if [ -n "$(df -T -b 2>/dev/null)" ]; then
df -T -b 2>/dev/null | sed 1d | grep -v -E 'devfs|tmpfs|map |/System/Volumes/' > "$KV_DIR/df_disks"
while IFS= read -r line; do
fields=($line)
fs_type=${fields[1]}
total_size=${fields[2]}
used_size=${fields[3]}
avail_size=${fields[4]}
mount_point=""
if [ "${#fields[@]}" -ge 9 ]; then
mount_point="${fields[*]:8}"
fi
if [ -n "$mount_point" ] && [ -n "$total_size" ]; then
DISKs="$DISKs$mount_point,$fs_type,$total_size,$used_size,$avail_size;"
fi
done < "$KV_DIR/df_disks"
else
df -b 2>/dev/null | sed 1d | grep -v -E 'devfs|tmpfs|map |/System/Volumes/' > "$KV_DIR/df_disks"
while IFS= read -r line; do
fields=($line)
total_size=$(( ${fields[1]:-0} * 512 ))
used_size=$(( ${fields[2]:-0} * 512 ))
avail_size=$(( ${fields[3]:-0} * 512 ))
mount_point=""
if [ "${#fields[@]}" -ge 6 ]; then
mount_point="${fields[*]:5}"
fi
# Detect filesystem type from mount point
fs_type=$(mount 2>/dev/null | grep " on $mount_point " | sed -E "s/.*\\(([^,)]*).*/\\1/" | head -1)
fs_type=${fs_type:-apfs}
if [ -n "$mount_point" ] && [ -n "$total_size" ]; then
DISKs="$DISKs$mount_point,$fs_type,$total_size,$used_size,$avail_size;"
fi
done < "$KV_DIR/df_disks"
fi
DISKs=$(echo -ne "$DISKs" | base64 | tr -d '\n\r\t ')
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) DISKs encoded" >> "$ScriptPath"/debug.log; fi
# Disk inodes
INODEs=""
df -i 2>/dev/null | sed 1d | grep -v -E 'devfs|tmpfs|map |/System/Volumes/' > "$KV_DIR/df_inodes"
while IFS= read -r line; do
fields=($line)
iused=${fields[5]:-0}
ifree=${fields[6]:-0}
itotal=$((iused + ifree))
mount_point=""
if [ "${#fields[@]}" -ge 9 ]; then
mount_point="${fields[*]:8}"
fi
if [ -n "$mount_point" ]; then
INODEs="$INODEs$mount_point,$itotal,$iused,$ifree;"
fi
done < "$KV_DIR/df_inodes"
INODEs=$(echo -ne "$INODEs" | base64 | tr -d '\n\r\t ')
# Disk IOPS
IOPS=""
IOPS_TIME_END=$(date +%s)
IOPS_TIME_DIFF=$((IOPS_TIME_END - IOPS_TIME_START))
if [ "$IOPS_TIME_DIFF" -le 0 ]; then IOPS_TIME_DIFF=1; fi
ioreg -c IOBlockStorageDriver -r -l -d 3 2>/dev/null | awk '
/IOBlockStorageDriver/{stats_r=""; stats_w=""; bsd=""}
/"Statistics"/{
r=$0; sub(/.*"Bytes \(Read\)"=/, "", r); sub(/[,}].*/, "", r); stats_r=r
w=$0; sub(/.*"Bytes \(Write\)"=/, "", w); sub(/[,}].*/, "", w); stats_w=w
}
/"BSD Name" = "disk[0-9]+"/{
b=$0; sub(/.*"BSD Name" = "/, "", b); sub(/".*/, "", b); bsd=b
if(bsd != "" && stats_r != "") print bsd ":" stats_r ":" stats_w
}
' | while IFS=: read disk rd_end wr_end; do
rd_start=$(kv_get "iops_r" "$disk" 0)
wr_start=$(kv_get "iops_w" "$disk" 0)
mnt=$(kv_get "iops_mnt" "$disk" "/")
rd_bps=$(echo | awk "{print (${rd_end:-0} - ${rd_start:-0}) / $IOPS_TIME_DIFF}" | awk '{printf "%18.0f",$1}' | xargs)
wr_bps=$(echo | awk "{print (${wr_end:-0} - ${wr_start:-0}) / $IOPS_TIME_DIFF}" | awk '{printf "%18.0f",$1}' | xargs)
echo "$mnt,$rd_bps,$wr_bps;"
if [ "$DEBUG" -eq 1 ]; then
echo -e "$ScriptStartTime-$(date +%T]) IOPS end: $disk ($mnt) ReadEnd=${rd_end:-0} WriteEnd=${wr_end:-0} ReadBps=$rd_bps WriteBps=$wr_bps" >> "$ScriptPath"/debug.log
fi
done > "$KV_DIR/iops_result"
IOPS=$(cat "$KV_DIR/iops_result" 2>/dev/null | tr -d '\n')
IOPS=$(echo -ne "$IOPS" | base64 | tr -d '\n\r\t ')
# Network final
NICS=""
IPv4=""
IPv6=""
for NIC in "${NetworkInterfacesArray[@]}"; do
nic_rx=$(kv_get "tRX" "$NIC" 0)
nic_tx=$(kv_get "tTX" "$NIC" 0)
RX=$(echo | awk "{print $nic_rx / $X}" | awk '{printf "%18.0f",$1}' | xargs)
TX=$(echo | awk "{print $nic_tx / $X}" | awk '{printf "%18.0f",$1}' | xargs)
NICS="$NICS$NIC,$RX,$TX;"
IPv4="$IPv4$NIC,$(ifconfig "$NIC" 2>/dev/null | grep "inet " | awk '{print $2}' | xargs | sed 's/ /,/g');"
IPv6="$IPv6$NIC,$(ifconfig "$NIC" 2>/dev/null | grep "inet6 " | grep -v "fe80" | awk '{print $2}' | sed 's/%.*//g' | xargs | sed 's/ /,/g');"
done
NICS=$(echo -ne "$NICS" | base64 | tr -d '\n\r\t ')
IPv4=$(echo -ne "$IPv4" | base64 | tr -d '\n\r\t ')
IPv6=$(echo -ne "$IPv6" | base64 | tr -d '\n\r\t ')
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Network encoded" >> "$ScriptPath"/debug.log; fi
# Port connections
CONN=""
if [ ${#ConnectionPortsArray[@]} -gt 0 ]; then
for cPort in "${ConnectionPortsArray[@]}"; do
cval=$(kv_get "conn" "$cPort" 0)
CON=$(echo | awk "{print $cval / $X}" | awk '{printf "%18.0f",$1}' | xargs)
CONN="$CONN$cPort,$CON;"
done
fi
CONN=$(echo -ne "$CONN" | base64 | tr -d '\n\r\t ')
# Temperature
TEMP=""
if [ "$(id -u)" -eq 0 ]; then
# Intel Macs: try powermetrics smc sampler for CPU die temperature
if command -v "powermetrics" > /dev/null 2>&1; then
TEMP_RAW=$(powermetrics --samplers smc -i 1 -n 1 2>/dev/null | grep "CPU die temperature" | grep -oE '[0-9.]+')
if [ -n "$TEMP_RAW" ]; then
TEMP_VAL=$(echo "$TEMP_RAW" | awk '{printf "%18.0f", $1 * 1000}' | xargs)
TEMP="CPU_die,$TEMP_VAL;"
fi
fi
# Apple Silicon / fallback: get SSD temperature from smartctl (NVMe SMART)
if [ -z "$TEMP" ] && command -v "smartctl" > /dev/null 2>&1; then
for disk in $(diskutil list 2>/dev/null | grep "^/dev/disk[0-9]" | grep "physical" | awk '{print $1}' | sort -u); do
SSD_TEMP=$(smartctl -A "$disk" 2>/dev/null | grep "^Temperature:" | grep -oE '[0-9]+')
if [ -n "$SSD_TEMP" ]; then
SSD_TEMP_MILLI=$((SSD_TEMP * 1000))
TEMP="${TEMP}Core_Average,$SSD_TEMP_MILLI;"
fi
done
fi
fi
TEMP=$(echo -ne "$TEMP" | base64 | tr -d '\n\r\t ')
# Services
SRVCS=""
if [ ${#CheckServicesArray[@]} -gt 0 ]; then
for svc in "${CheckServicesArray[@]}"; do
svc_status=$(kv_get "srvcs" "$svc" 0)
# Re-check
svc_status=$((svc_status + $(servicestatus "$svc")))
if [ "$svc_status" -eq 0 ]; then
SRVCS="$SRVCS$svc,0;"
else
SRVCS="$SRVCS$svc,1;"
fi
done
fi
SRVCS=$(echo -ne "$SRVCS" | base64 | tr -d '\n\r\t ')
# RAID
RAID=""
ZP=""
RAID=$(echo -ne "$RAID" | base64 | tr -d '\n\r\t ')
ZP=$(echo -ne "$ZP" | base64 | tr -d '\n\r\t ')
# Drive Health
DH=""
if [ "${CheckDriveHealth:-0}" -gt 0 ]; then
if command -v "smartctl" > /dev/null 2>&1; then
for disk in $(diskutil list 2>/dev/null | grep "^/dev/disk[0-9]" | grep "physical\|external" | awk '{print $1}' | sort -u); do
DHealth=$(smartctl -A "$disk" 2>/dev/null)
if [ -n "$DHealth" ] && echo "$DHealth" | grep -q -E 'Attribute|SMART'; then
# Retry smartctl -H up to 3 times on IOKit failures (transient macOS issue)
DHHealth=""
for retry in 1 2 3; do
DHHealth=$(smartctl -H "$disk" 2>&1)
if echo "$DHHealth" | grep -q "PASSED\|FAILED"; then
break
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) smartctl -H $disk retry $retry" >> "$ScriptPath"/debug.log; fi
sleep 0.5
done
DHealth="$DHHealth\n$DHealth"
DHealth=$(echo -ne "$DHealth" | base64 | tr -d '\n\r\t ')
DInfo=$(smartctl -i "$disk" 2>/dev/null)
DModel=$(echo "$DInfo" | grep -i -E "Device Model:|Model Number:|Product:" | head -1 | awk -F':' '{print $2}' | xargs)
DSerial=$(echo "$DInfo" | grep -i "Serial Number:" | head -1 | awk -F':' '{print $2}' | xargs)
diskname=${disk##*/}
DH="${DH}1,${diskname},$DHealth,$DModel,$DSerial;"
fi
done
fi
fi
DH=$(echo -ne "$DH" | base64 | tr -d '\n\r\t ')
# Running Processes
RPS1=""
RPS2=""
if [ "${RunningProcesses:-0}" -gt 0 ]; then
if [ -f "$ScriptPath"/running_proc.txt ]; then
RPS1=$(cat "$ScriptPath"/running_proc.txt)
fi
RPS2=$(ps -Ao pid,ppid,uid,user,pcpu,pmem,etime,comm 2>/dev/null | tail -n +2)
RPS2=$(echo -ne "$RPS2" | base64 | tr -d '\n\r\t ')
echo "$RPS2" > "$ScriptPath"/running_proc.txt
fi
# Custom Variables
CV=""
if [ -n "$CustomVars" ]; then
if [ -s "$ScriptPath"/"$CustomVars" ]; then
CV=$(cat "$ScriptPath"/"$CustomVars" | base64 | tr -d '\n\r\t ')
fi
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) CV: $CV" >> "$ScriptPath"/debug.log; fi
# Outgoing PING
OPING=""
if [ -n "$OutgoingPings" ]; then
wait
if [ -f "$ScriptPath"/ping.txt ]; then
OPING=$(grep -v '^$' "$ScriptPath"/ping.txt | tr -d '\n' | base64 | tr -d '\n\r\t ')
rm -f "$ScriptPath"/ping.txt
fi
fi
# Secured Connection
if [ "${SecuredConnection:-1}" -gt 0 ]; then
CurlSecure=""
else
CurlSecure="--insecure"
fi
# Current time/date
Time=$(date "+%Y-%m-%d %T %Z" | base64 | tr -d '\n\r\t ')
# Prepare JSON
json='{"version":"'"$Version"'","SID":"'"$SID"'","agent":"3","user":"'"$User"'","os":"'"$OS"'","kernel":"'"$Kernel"'","hostname":"'"$Hostname"'","time":"'"$Time"'","reqreboot":"'"$RequiresReboot"'","uptime":"'"$Uptime"'","cpumodel":"'"$CPUModel"'","cpusockets":"'"$CPUSockets"'","cpucores":"'"$CPUCores"'","cputhreads":"'"$CPUThreads"'","cpuspeed":"'"$CPUSpeed"'","cpu":"'"$CPU"'","wa":"'"$CPUwa"'","st":"'"$CPUst"'","us":"'"$CPUus"'","sy":"'"$CPUsy"'","load1":"'"$loadavg1"'","load5":"'"$loadavg5"'","load15":"'"$loadavg15"'","ramsize":"'"$RAMSize"'","ram":"'"$RAM"'","ramswapsize":"'"$RAMSwapSize"'","ramswap":"'"$RAMSwap"'","rambuff":"'"$RAMBuff"'","ramcache":"'"$RAMCache"'","disks":"'"$DISKs"'","inodes":"'"$INODEs"'","iops":"'"$IOPS"'","raid":"'"$RAID"'","zp":"'"$ZP"'","dh":"'"$DH"'","nics":"'"$NICS"'","ipv4":"'"$IPv4"'","ipv6":"'"$IPv6"'","conn":"'"$CONN"'","temp":"'"$TEMP"'","serv":"'"$SRVCS"'","cust":"'"$CV"'","oping":"'"$OPING"'","rps1":"'"$RPS1"'","rps2":"'"$RPS2"'"}'
if [ "$DEBUG" -eq 1 ]; then
echo -e "$ScriptStartTime-$(date +%T]) JSON:\n$json" >> "$ScriptPath"/debug.log
fi
# Post data using curl
jsoncomp=$(echo -ne "$json" | gzip -c | base64 | tr -d '\n\r\t ' | sed 's/\//%2F/g' | sed 's/+/%2B/g')
if [ "$DEBUG" -eq 1 ]; then
echo -e "$ScriptStartTime-$(date +%T]) Posting data via curl" >> "$ScriptPath"/debug.log
curl -v --retry 3 --retry-delay 1 --max-time 15 -X POST -d "j=$jsoncomp" $CurlSecure https://sm.hetrixtools.net/v2/ >> "$ScriptPath"/debug.log 2>&1
echo -e "$ScriptStartTime-$(date +%T]) Data posted" >> "$ScriptPath"/debug.log
else
curl -s --retry 3 --retry-delay 1 --max-time 15 -X POST -d "j=$jsoncomp" $CurlSecure https://sm.hetrixtools.net/v2/ &> /dev/null
fi