-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGuardUpdater.sh
More file actions
117 lines (97 loc) · 3.46 KB
/
Copy pathGuardUpdater.sh
File metadata and controls
117 lines (97 loc) · 3.46 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
#!/bin/bash
## Config ###################################
dbHost=127.0.0.1 #Mysql Host name/Ip
dbUser=user #Mysql user
dbPassword=password #Mysql user's pass
dbName=SphereGuard #Mysql database name
hostNumberID=1324 #Unique ID of your watched server
hddToWatch=/dev/sda #temp form this hdd
isLVMStorage=0 #set to 1 if your storage is using lvm
#############################################
## Requirement check ################################
if [ -z $(command -v cat) ]
then
echo "cat is required !"
echo "Exiting"
exit 1
fi
if [ -z $(command -v grep) ]
then
echo "grep is required !"
echo "Exiting"
exit 1
fi
if [ -z $(command -v pvdisplay) ] && [ "$isLVMStorage" = 1 ]
then
echo "pvdisplay is required !"
echo "Exiting"
exit 1
fi
if [ -z $(command -v sensors) ]
then
echo "lm-sensors is required !"
echo "Exiting"
exit 1
fi
if [ -z $(command -v hddtemp) ]
then
echo "hddtemp is required !"
echo "Exiting"
exit 1
fi
#############################################
## RAM USAGE ################################
Ram=`cat /proc/meminfo | grep 'MemFree:'`
Ramf=${Ram#*:}
FreeRam=${Ramf/kB/}
Ram2=`cat /proc/meminfo | grep 'MemTotal:'`
Ramt=${Ram2#*:}
RamTotal=${Ramt/kB/}
let "UsedMem=100-$FreeRam*100/RamTotal"
#############################################
## CPU Usage ################################
CpuUsage=$(top -bn 1 | awk '{print $9}' | tail -n +8 | awk '{s+=$1} END {print s}')
#############################################
## Disk Usage ###############################
if [ "$isLVMStorage" = 1 ]
then
## Disk Usage (lvmVolume)
dT=`pvdisplay | grep 'Total PE'`
dTotal=${dT#*PE}
let "DiskTotal=4*$dTotal"
dF=`pvdisplay | grep 'Free PE'`
dFree=${dF#*PE}
let "DiskFree=4*$dFree"
DiskUs=$(echo 100-$DiskFree*100/$DiskTotal | bc -l)
DiskUsage=$(echo $DiskUs | cut -d"." -f1);
else
## Disk Usage (No-lvmVolume)
DiskCapacity=$(df -Ph $hddToWatch | tail -1 | awk '{print $2}' | cut -d'G' -f1)
DiskCurrentUsage=$(df -Ph $hddToWatch | tail -1 | awk '{print $4}' | cut -d'G' -f1)
DiskUsage=$(($DiskCapacity/($DiskCapacity-$DiskCurrentUsage)))
fi
#############################################
## CPU Temp ##############################
tmp=$(sensors | grep "Core 0" | awk '{print $3}')
t=${tmp#*+}
tempCPU=${t/°C/}
##########################################
## HDD Temp ##############################
tmpH=`sudo hddtemp $hddToWatch | awk '{print $4}'`
tHD=${tmpH#*+}
tempHD=${tHD/°C/}
##########################################
## HDD Temp ##############################
localIp=$(ip a s|sed -ne '/127.0.0.1/!{s/^[ \t]*inet[ \t]*\([0-9.]\+\)\/.*$/\1/p}')
##########################################
## SQL REQUEST ###########################
mysql --host=$dbHost --user=$dbUser --password=$dbPassword $dbName << EOF
UPDATE host SET ip="${localIp}" WHERE pk_host="${hostNumberID}";
INSERT INTO performance (fk_type, value, fk_host, date) VALUES ('1','${UsedMem}','${hostNumberID}',NOW());
INSERT INTO performance (fk_type, value, fk_host, date) VALUES ('2','${CpuUsage}','${hostNumberID}',NOW());
INSERT INTO performance (fk_type, value, fk_host, date) VALUES ('3','${DiskUsage}','${hostNumberID}',NOW());
INSERT INTO performance (fk_type, value, fk_host, date) VALUES ('4','${tempCPU}','${hostNumberID}',NOW());
INSERT INTO performance (fk_type, value, fk_host, date) VALUES ('5','${tempHD}','${hostNumberID}',NOW());
exit
EOF
##########################################