forked from zyclonite/newrelic-openwrt-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewrelic.sh
executable file
·111 lines (102 loc) · 2.78 KB
/
newrelic.sh
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
#!/bin/ash
licensekey="insert your license key"
host="insert router hostname"
name="insert router name"
version="1.0.1"
SendMetrics () {
curl -sk -o /dev/null https://platform-api.newrelic.com/platform/v1/metrics \
-H "X-License-Key: $licensekey" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-X POST -d '{
"agent": {
"host" : "'$host'",
"version" : "'$version'"
},
"components": [
{
"name": "'$name'",
"guid": "net.zyclonite.newrelic.openwrt",
"duration" : 60,
"metrics" : {
"Component/System/Load[load]": '$1',
"Component/Cpu/User[percent]": '$2',
"Component/Cpu/System[percent]": '$3',
"Component/Cpu/IOWait[percent]": '$4',
"Component/Memory/Used[bytes]": '$5',
"Component/Memory/Swap[bytes]": '$6',
"Component/Memory/Free[percent]": '$7'
}
}
]
}'
}
GetCpuStats () {
for c in 1 2 3
do
read -r CPU </proc/stat
CPU=$( echo $CPU | cut -d " " -f2- )
TOTAL=0
for t in $CPU
do
TOTAL=$(($TOTAL+$t))
done
DIFF_TOTAL=$(($TOTAL-${PREV_TOTAL:-0}))
i=0
for number in $CPU
do
PREV=$( echo $PREV_STAT | cut -d " " -f$(($i+1)) )
OUT_INT=$((1000*($number-${PREV:-0})/$DIFF_TOTAL))
eval OUT$(($i*2))="'$(($OUT_INT/10))'"
eval OUT$(($i*2+1))="'$(($OUT_INT%10))'"
i=$(($i+1))
done
PREV_STAT="$CPU"
PREV_TOTAL="$TOTAL"
if [ $c -lt 3 ]
then
sleep 1
fi
done
user=$(printf '%3d.%1d ' "$OUT0" "$OUT1")
system=$(printf '%3d.%1d ' "$OUT4" "$OUT5")
iowait=$(printf '%3d.%1d ' "$OUT8" "$OUT9")
}
GetMemoryStats () {
free=0
total=0
buffer=0
cached=0
swapfree=0
swaptotal=0
while IFS=":" read -r a b
do
case "$a" in
MemTotal*) total=$( echo $b | cut -d " " -f1 )
;;
MemFree*) free=$( echo $b | cut -d " " -f1 )
;;
Buffers*) buffers=$( echo $b | cut -d " " -f1 )
;;
SwapTotal*) swaptotal=$( echo $b | cut -d " " -f1 )
;;
SwapFree*) swapfree=$( echo $b | cut -d " " -f1 )
;;
Cached*) cached=$( echo $b | cut -d " " -f1 )
esac
done <"/proc/meminfo"
realfree=$(($free+$buffers+$cached))
realused=$(($total-$realfree))
swapused=$(($swaptotal-$swapfree))
FREE_INT=$((1000*$realfree/$total))
FREE0=$(($FREE_INT/10))
FREE1=$(($FREE_INT%10))
freepercent=$(printf '%3d.%1d ' "$FREE0" "$FREE1")
}
GetLoadStats () {
load=$( cat /proc/loadavg | cut -d " " -f1 )
}
GetLoadStats
GetCpuStats
GetMemoryStats
SendMetrics $load $user $system $iowait $(($realused*1024)) $(($swapused*1024)) $freepercent