host-monitor.sh 14.9 KB
Newer Older
Peter Möller's avatar
Peter Möller committed
1
#!/bin/bash
2
3
# Getting data for the host web.cs.lth.se"
# 2021-05-17/PM
Peter Möller's avatar
Peter Möller committed
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

MONITOR_RESTAPI_URL=https://monitor.cs.lth.se/api/v1

source /opt/monitoring/host-monitor.template

# 1. Generating data

#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# sysinfo
# Operating System (OS='Ubuntu 20.04.1 LTS'):
# Find out distro
# Fist: look at the /etc/*-release files
OS="$(less $(ls -1 /etc/mageia-release /etc/centos-release /etc/redhat-release /etc/gentoo-release /etc/fedora-release 2>/dev/null | head -1) 2>/dev/null)"
# If no such file, look for /etc/os-release
if [ -z "$OS" ]; then
    [[ -f /etc/os-release ]] && OS="$(less /etc/os-release | grep PRETTY_NAME | cut -d\" -f2)"
fi
# If no such file, look for /etc/lsb-release
if [ -z "$OS" ]; then
    [[ -f /etc/lsb-release ]] && OS="$(less /etc/os-release | grep DISTRIB_DESCRIPTION | cut -d\" -f2)"
fi
# If no such file, look for /proc/version
if [ -z "$OS" ]; then
    [[ -f /proc/version ]] && OS="$(less /proc/version | cut -d\( -f1)"
fi
# OK, so it an unknown system
if [ -z "$OS" ]; then
    OS="Unknown Linux-distro"
fi
##OS="$(lsb_release -ds)"

# Kind of CPU (CPUModel='Intel® Xeon® CPU E5-2680 v4 @ 2.40GHz'):
CPUModel="$(egrep "^model name" /proc/cpuinfo | sort -u | cut -d: -f2 | sed -e 's/^ //' -e 's/(R)/®/g')"
# Number of CPU:s (NbrCPUs=2):
NbrCPUs=$(egrep "^processor" /proc/cpuinfo | wc -l)
# Size of memory [kB]
root's avatar
root committed
40
41
42
43
##RAM=$(/usr/sbin/dmidecode -t 17 | grep "Size.*MB" | awk '{s+=$2} END {print s * 1024}')    # Ex: RAM=4194304
##if [ $RAM -eq 0 ]; then
    RAM=$(egrep -i "^MemTotal" /proc/meminfo | awk '{print $2}')
##fi
Peter Möller's avatar
Peter Möller committed
44
45
46
47
48
49
50
# Kind of memory (ECC='No ECC'  or  ECC='Multi-bit ECC'):
ECC="$(/usr/sbin/dmidecode -t memory | egrep "Error Correction Type" | sort -u | cut -d: -f2 | sed -e 's/^ //' -e 's/None/No ECC/')"
# What kind of authentication?   Ex: Authentication=Lucat
Authentication="$(if [ -n "$(egrep -v "#|^$" /etc/pam.d/common-account | egrep pam_krb5.so)" -a -n "$(egrep "default_realm = UW.LU.SE" /etc/krb5.conf)" ]; then echo Lucat; else echo Standalone; fi)"
AuthStr="\"authentication\": \"${Authentication}\""     # Ex: AuthStr='"authentication": "Lucat"'
# Firewall.   Ex: Firewall=ufw
Firewall="$(systemctl list-units --type=service --state=active | egrep -i "^\ *(iptables|firewalld|ufw)" | awk '{print $1}' | cut -d\. -f1)"
51
52
53
54
55
56
57
# Is it acive?
case "$Firewall" in
    "ufw") FirewallStatus="$(/usr/sbin/ufw status 2>/dev/null | awk '/Status:/ {print $NF}')";;
    "firewalld") FirewallStatus="$(firewall-cmd --state)";;
    "*") FirewallStatus="unknown status";;
esac
FirewallStr="${Firewall} ($FirewallStatus)"    # Ex: FirewallStr='ufw (inactive)'
Peter Möller's avatar
Peter Möller committed
58
# Disks
59
Filesystems="$(df -kB1 -T -t xfs -t ext4 | egrep "\/" | awk '{print $1" "$2" "$3" "$7}')"
Peter Möller's avatar
Peter Möller committed
60
# Ex: a number of rows with
61
62
63
# /dev/mapper/vg0-ubuntu ext4  527366881280 /Websites
#(Filesystem             Type  1B-blocks    Mounted on)
LVMDisks="$(lsblk -b | egrep -i lvm | awk '{print $4" "$NF}' | egrep -vi swap | sort -u)"
Peter Möller's avatar
Peter Möller committed
64
# Ex:
65
66
67
# LVMDisks='13950255104 /
# 53682896896 /var/log/apache2
# 536866717696 /Websites'
Peter Möller's avatar
Peter Möller committed
68
69
70

# Flags
# SELinux
root's avatar
root committed
71
SELinux="$(if sestatus &>/dev/null; then sestatus | egrep "^Current mode:|^SELinux status:" | awk '{print $NF}'; else echo "Not present"; fi)"
Peter Möller's avatar
Peter Möller committed
72
73
74
75
76
# CPU bugs
CPUBugs="$(egrep "^bugs" /proc/cpuinfo | cut -d: -f2 | cut -c2- | uniq)"
# Ex: CPUBugs='cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit'
Kernel="$(uname -r 2>/dev/null)"            # Ex: Kernel=5.4.0-62-generic
Architecture="$(uname -m 2>/dev/null)"      # Ex: Architecture=x86_64
root's avatar
root committed
77
Uptime="$(awk '{print int($1/86400)}' /proc/uptime)"
root's avatar
root committed
78
79
80
81
82
83
84
85
86
87
88
89
# unattended-upgrades
case $(systemctl status unattended-upgrades &>/dev/null; echo $?) in
    0) UnattendedUpgrades="Active";;
    1) UnattendedUpgrades="Generic failure or unspecified error";;
    2) UnattendedUpgrades="Invalid or excess arguments";;
    3) UnattendedUpgrades="Unimplemented";;
    4) UnattendedUpgrades="Not installed";;
    5) UnattendedUpgrades="Not installed";;
    6) UnattendedUpgrades="Not configured";;
    7) UnattendedUpgrades="Not running";;
esac

root's avatar
root committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Is reboot required?
UbuntuReboot() {
    if [ -f /var/run/reboot-required ]; then
        RebootRequired="Yes"
    else
        RebootRequired="No"
    fi
}
RedHatReboot() {
    if [ -x /bin/needs-restarting ]; then
        if [ -n "$(needs-restarting -r 2>/dev/null | egrep -i "reboot" | egrep "not necessary")" ]; then
            RebootRequired="No"
        else
            RebootRequired="Yes"
        fi
    fi
}
case "$(echo "${OS,,}" | cut -c-6)" in
    "centos") RedHatReboot;;
    "red ha") RedHatReboot;;
    "ubuntu") UbuntuReboot;;
esac


Peter Möller's avatar
Peter Möller committed
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
# Are we in a Virtual environment? This one is a bit tricky since there are many ways to cover this.
# Read this for more info: http://unix.stackexchange.com/questions/89714/easy-way-to-determine-virtualization-technology
VMenv="$(dmesg 2>/dev/null | grep -i " Hypervisor detected: " 2>/dev/null | cut -d: -f2 | sed 's/^ *//')"
# Ex: VMenv='VMware'
if [ -z "${USER/root/}" -o -z "${UID/0/}" ]; then
    # Get a better VMenv if found
    if [ -n "$VMenv" ]; then
        VMenv="$(/usr/sbin/dmidecode -s system-product-name 2>/dev/null)"
    fi
    # Ex: VMenv='VMware Virtual Platform'
    # It may still be a VM environment
    if [ -z "$VMenv" ]; then
        VMenv="$(virt-what 2>/dev/null)"
        # Ex: VMenv='vmware'
    fi
    if [ -z "$VMenv" ]; then
        VMenv="$(if [ -n "$(grep "^flags.*\ hypervisor\ " /proc/cpuinfo)" ]; then echo "VM environment detected"; fi)"
        # Ex: VMenv='VM environment detected'
    fi
fi

# Get more platform data
PlatformManufacturer="$(/usr/sbin/dmidecode -t 2 2>/dev/null | egrep "^\s*Manufacturer:" | cut -d: -f2 | cut -c2-)"   # Ex: PlatformManufacturer=LNVO
PlatformType="$(/usr/sbin/dmidecode -t 2 2>/dev/null | egrep "^\s*Type:" | cut -d: -f2 | cut -c2-)"                   # Ex: PlatformType=Motherboard

# Network interfaces
IFs=$(ip a |awk '/state UP/{print $2}' | sed 's/://')
# Ex: IFs='ens192
# br-8ebd54cbbafb
# veth079b7c8@if123'

# fail2ban
case $(systemctl status fail2ban &>/dev/null; echo $?) in
     0) Fail2Ban="Active";;
     1) Fail2Ban="Generic failure or unspecified error";;
     2) Fail2Ban="Invalid or excess arguments";;
     3) Fail2Ban="Unimplemented";;
     4) Fail2Ban="Not installed";;
     5) Fail2Ban="Not installed";;
     6) Fail2Ban="Not configured";;
     7) Fail2Ban="Not running";;
esac


#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Security patches
SecurityPatches="$(apt-get update &>/dev/null && apt list --upgradable 2>/dev/null | egrep "\-security" | sed -e 's/\[upgradable from: //' -e 's/\]//' | tr '[/ ]' '\t')"
# List of: Package  Channel CurrentVer  Arch    NewVersion
# intel-microcode focal-updates,focal-security,focal-updates,focal-security 3.20210216.0ubuntu0.20.04.1 amd64 3.20201110.0ubuntu0.20.04.2
# libhogweed5 focal-updates,focal-security,focal-updates,focal-security 3.5.1+really3.5.1-2ubuntu0.1  amd64 3.5.1+really3.5.1-2
# libmysqlclient21  focal-updates,focal-security,focal-updates,focal-security 8.0.25-0ubuntu0.20.04.1 amd64 8.0.22-0ubuntu0.20.04.3


#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Listening ports
ListeningPorts="$(lsof +c 0 -i 4 -i 6 -n -P 2>/dev/null | grep LISTEN | awk '{print $1" "$3" "$5" "$8" "$9 }' | sed 's/\(.*\):/\1 /' | sort -u)"
# List of: Application User IPver Proto Servicerange Port
# apache2 root IPv6 TCP * 88
# apache2 www-data IPv6 TCP * 88
# cupsd root IPv4 TCP 127.0.0.1 631
# cupsd root IPv6 TCP [::1] 631
# dnsmasq nobody IPv4 TCP 127.0.1.1 53
# docker-proxy root IPv6 TCP * 4000
# dsmcad root IPv4 TCP * 1581
# dsmcad root IPv4 TCP * 2123

#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Firewall -- No longer used as of 2021-05-28
FWrules="$(ufw status verbose 2>/dev/null | egrep "(ALLOW|DENY) IN" | awk '{print $1}' | cut -d\/ -f1 | egrep -vi "^anywhere|out" | tr ',' $'\n' | sort -nu)"
# Ex:
# FWrules='22
# 161
# 2500:5000
# 5666'

####################################################################################################################################################################
####################################################################################################################################################################

# 2. Generate JSON-parts

#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# sysinfo
# Create a string for the CPU Bugs (to make the final assemble shorter)
CPUBugsStr="$(if [ -n "$CPUBugs" ]; then echo ", \"cpu-bugs:${CPUBugs// /\", \"cpu-bugs:}\""; fi)"
# Assemble the Flags string
root's avatar
root committed
199
Flags="\"selinux:$SELinux\", \"kernel:${Kernel}\"$CPUBugsStr, \"arch:${Architecture:---}\", \"fail2ban:$Fail2Ban\", \"Uptime-days:$Uptime\", \"Reboot-required:$RebootRequired\", \"unattended-upgrades:$UnattendedUpgrades\""
Peter Möller's avatar
Peter Möller committed
200
201
202

# partitions
OUTPUT=""
203
while read -r Filesystem Type KBlocks MountP
Peter Möller's avatar
Peter Möller committed
204
205
206
do
    if [ -n "$(echo "$LVMDisks" | egrep "\s${MountP}$")" ]; then
        LVM="LVM"
207
208
209
210
211
212
213
        #case $(echo "$LVMDisks" | egrep "\s${MountP}$" | awk '{print $1}' | sed 's/[0-9.]*//') in
            #M) ScaleFactor=1024;;
            #G) ScaleFactor=1048576;;
            #T) ScaleFactor=1073741824;;
        #esac
        #Kblocks=$(echo "scale=0; $(echo "$LVMDisks" | egrep "\s${MountP}$" | awk '{print $1}' | sed 's/[MGT]//') * $ScaleFactor" | bc -l)
        Kblocks=$(echo "scale=0; $(echo "$LVMDisks" | egrep "\s${MountP}$" | awk '{print $1}') / 1024" | bc -l)
Peter Möller's avatar
Peter Möller committed
214
215
    else
        LVM=""
216
        Kblocks=$(echo "scale=0; $KBlocks / 1024" | bc -l)
Peter Möller's avatar
Peter Möller committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    fi
    OUTPUT+="{ \"partition\": \"$Filesystem\", \"mount-point\":\"$MountP\", \"type\":\"$Type\", \"total-kb\":$Kblocks, \"storage\":\"${LVM:-direct}\" },"
done <<< "$Filesystems"
# Assemble the complete string (minus the final comma)
FilesystemStr="\"partitions\": [ ${OUTPUT%%,} ]"

# Network interfaces
OUTPUT=""
for iname in $IFs
do
    IP4=$(ip -4 -o a show ${iname%%@if[0-9]*} | awk '{print $4}')
    IP6=$(ip -6 -o a show ${iname%%@if[0-9]*} | awk '{print $4}')
    MAC="$(ip link show dev ${iname%%@if[0-9]*} |awk '/link/{print $2}')"
    MAC_P=$(ethtool -P ${iname%%@if[0-9]*} | awk '{print $NF}')
    Speed="$(ethtool ${iname%%@if[0-9]*} | grep "Speed:" | awk '{print $NF}')"
232
    OUTPUT+="{ \"interface\": \"$iname\", \"ip4\": \"${IP4}\", \"ip6\": \"${IP6}\", \"mac\": \"${MAC}\", \"mac-p\": \"${MAC_P}\", \"speed\": \"${Speed/000Mb/ Gb}\" },"
Peter Möller's avatar
Peter Möller committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
done
NIStr="\"network-interfaces\": [ $(echo "${OUTPUT}" | sed 's/,$//') ]"
# Ex: NIStr='"network-interfaces": [ { "interface": "ens192", "ip4": "130.235.16.11/23", "ip6": "fe80::250:56ff:feb6:b194/64", "mac": "00:50:56:b6:b1:94", "mac-p": "00:50:56:b6:b1:94", "speed": "10000Mb/s" } ]'

# Platform
if [ -n "$VMenv" ]; then
    PlatformStr="\"platform\": { \"virtualized\": true, \"name\": \"$VMenv\" }"
else
    PlatformStr="\"platform\": { \"virtualized\": false, \"name\": \"type:$PlatformType\" }"
fi
# network
AliasName=", \"alias\": [\"$DNSAlias\"] "
NetworkStr="\"network\": { \"hostname\": \"$(hostname -f)\"$AliasName }"

CPUstr="\"cpu\": { \"name\": \"${CPUModel:---}\", \"threads\": ${NbrCPUs:---} }"
RAMStr="\"memory\": { \"total-kb\": ${RAM:---}, \"type\": \"${ECC:---}\" }"
249
SysinfoStr="\"sysinfo\": { \"os\": \"${OS:---}\", \"authentication\":\"${Authentication}\", \"firewall\":\"${FirewallStr}\", \"flags\": [ ${Flags} ], $CPUstr, $RAMStr, $FilesystemStr, $NIStr, $PlatformStr, $NetworkStr }"
Peter Möller's avatar
Peter Möller committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Ex:

#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# listening ports
if [ -n "$ListeningPorts" ]; then
    OUTPUT=""
    while read -r line
    do
      read Application User IPver Proto Binding Port <<< "$line"
      # Determine if there's some form of firewall rule that covers $Port:
      FWrule="null"
      if [ -n "$Firewall" ]; then
          if [ -n "$(echo "$FWrules" | egrep "$Port")" ]; then
              FWrule="true"
          else
              FWrule="false"
          fi
      fi
      #OUTPUT+="{ \"binding\": \"${Binding:--}\", \"port\": ${Port:--}, \"process-owner\": \"${Application:--}\", \"process-user\": \"${User:--}\", \"IPver\": \"${IPver:--}\", \"protocol\": \"${Proto:--}\" },"
269
      OUTPUT+="{ \"binding\": \"${Binding:--}\", \"port\": ${Port:--}, \"process-owner\": \"${Application:--}\", \"process-user\": \"${User:--}\", \"protocol\": \"${IPver}:${Proto:--}\", \"firewall-rule\": $FWrule },"
Peter Möller's avatar
Peter Möller committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    done <<< "$ListeningPorts"
    # Assemble the complete string (minus the final comma)
    ListenStr=", \"listen-ports\": [ ${OUTPUT%%,} ]"
else
    ListenStr=""
fi
# Ex:

#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# security patches
if [ -n "$SecurityPatches" ]; then
    OUTPUT=""
    while read -r Package Sources InstalledVer Arch NewVer
    do
        Source="$(echo -e ${Sources//,/'\n'} | sort -u | sed -z 's/\n/,/g;s/,$/\n/')"
        STR="\"${Sources//,/\",\"}\""
        SourceArr="$(echo -e ${STR//,/'\n'} | sort -u | sed -z 's/\n/,/g;s/,$/\n/')"
        OUTPUT+="{ \"installed-version\": \"$InstalledVer\", \"new-version\": \"$NewVer\", \"package\": \"$Package\", \"sources\": [ $SourceArr ] },"
    done <<< "$SecurityPatches"
    # Assemble the complete string (minus the final comma)
    SecurityPatchStr=", \"security-patches\": [ ${OUTPUT%%,} ]"
else
292
    SecurityPatchStr=", \"security-patches\": [ ]"
Peter Möller's avatar
Peter Möller committed
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
fi
# Ex:

#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# firewall rules
#OUTPUT=""
#if [ -n "$FWrules" ]; then
    #while read -r Rule Binding
    #do
        #if echo "$Rule" | egrep ":" &>/dev/null ; then
            #OUTPUT+="{ \"binding\": \"${Binding:-*}\", \"port\": [$(echo $Rule | cut -d: -f1), $(echo $Rule | cut -d: -f2)] },"
        #else
            #OUTPUT+="{ \"binding\": \"${Binding:-*}\", \"port\": $Rule },"
        #fi
    #done <<< "$FWrules"
    #FWrulesStr=", \"firewall-rules\": [ $(echo "$OUTPUT" | sed 's/,$//') ]"
#else
    #FWrulesStr=""
#fi
# Ex:
# FWrulesStr=', "firewall-rules": [ { "binding": "*", "port": 22 },{ "binding": "*", "port": 161 },{ "binding": "*", "port": [2500, 5000] },{ "binding": "*", "port": 5666 } ]'


# 3. Build observation string and check for errors
OBSERVATIONS="{ ${SysinfoStr//\"--\"/null}${ListenStr//\"--\"/null}${SecurityPatchStr} }"

# 4. Upload results
curl --silent -X POST "$MONITOR_RESTAPI_URL/hosts/monitor?token=$SOURCE_TOKEN" \
    -H  "accept: application/json" \
    -H  "Content-Type: application/json" \
    -d "$OBSERVATIONS"