-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmonitor
More file actions
executable file
·133 lines (118 loc) · 4.53 KB
/
Copy pathmonitor
File metadata and controls
executable file
·133 lines (118 loc) · 4.53 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
#!/bin/bash -e
# SPDX-License-Identifier: AGPL-3.0-only
UPTIME="$(cut -d. -f1 /proc/uptime)"
# get machine mac address
NETDEV=$(ip route show default | awk '/default/ {print $5}' | head -n 1)
test -n "$NETDEV" && MACADDR="$(tr -d : < "/sys/class/net/$NETDEV/address")"
test -z "$MACADDR" && MACADDR="00:00:00:00:00:00"
WIFI_SIGNAL=""
WIFI_BSSID=""
if [ -n "$NETDEV" ] && [ -d "/sys/class/net/$NETDEV/wireless" ]; then
if command -v iw >/dev/null 2>&1; then
IW_LINK="$(iw dev "$NETDEV" link 2>/dev/null)"
if printf '%s' "$IW_LINK" | grep -qi "Connected"; then
WIFI_BSSID="$(printf '%s\n' "$IW_LINK" | awk '/Connected to/ {print $3}' | head -n 1)"
WIFI_SIGNAL="$(printf '%s\n' "$IW_LINK" | awk '/signal/ {print $2}' | head -n 1)"
fi
fi
if ( [ -z "$WIFI_SIGNAL" ] || [ -z "$WIFI_BSSID" ] ) && command -v iwconfig >/dev/null 2>&1; then
IWCONFIG_OUTPUT="$(iwconfig "$NETDEV" 2>/dev/null)"
if [ -z "$WIFI_BSSID" ]; then
WIFI_BSSID="$(printf '%s\n' "$IWCONFIG_OUTPUT" | awk -F'=' '/Access Point/ {print $2}' | awk '{print $1}' | head -n 1)"
fi
if [ -z "$WIFI_SIGNAL" ]; then
WIFI_SIGNAL="$(printf '%s\n' "$IWCONFIG_OUTPUT" | awk -F'=' '/Signal level/ {print $3}' | awk '{print $1}' | head -n 1)"
fi
fi
fi
test -z "$OS" && OS="$(cat /etc/issue.net | awk '{print $1}')"
test -z "$OS" && OS="$(cat /etc/issue | awk '{print $1}')"
test -z "$OS" && OS="$(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2 | sed 's/"//g')"
test -z "$OS" && OS="$(cat /etc/os-release | grep NAME | cut -d= -f2 | sed 's/"//g')"
test -z "$OS" && OS="unknown"
test -r /icpc/version && VERSION="$(grep -Po "Revision: \K\S*" /icpc/version)"
test -z "$VERSION" && VERSION="$(grep -Po "version=\K\S*" /proc/cmdline)"
test -z "$VERSION" && VERSION="devel"
# get machine kernel version
KERNEL="$(uname -r)"
test -r /etc/hostname && SEATS="$(cat /etc/hostname)"
test -r /proc/cpuinfo && CPU="$(cat /proc/cpuinfo | grep 'model name' | uniq | awk -F: '{print $2}' | sed 's/^[ \t]*//g')"
test -r /proc/cpuinfo && CPU="$(grep -c processor /proc/cpuinfo) x $CPU"
test -z "$CPU" && CPU="unknown"
# replace spaces with underscores
CPU="${CPU// /_}"
# get machine cpu used percentage
CPUUSED="$(top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8}')"
# get machine load average
LOAD="$(cat /proc/loadavg | awk '{print $1" "$2" "$3}')"
# get machine memory total
MEM="$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')"
MemFree="$(cat /proc/meminfo | grep MemFree | awk '{print $2}')"
MEMUSED="$(($MEM-$MemFree))"
# get active window title
SESSIONS=$(find /tmp/.X11-unix -type s -print0 | xargs -n1 -0 basename | sed 's/^X//' | tr -dc "0-4\n")
for D in $SESSIONS; do
export DISPLAY=":$D"
if xset -q &>/dev/null; then
break
fi
unset DISPLAY
done
_XPROP_RESULT=$(xprop -notype -id "$(xprop -root -f _NET_ACTIVE_WINDOW 0x " \$0\n" _NET_ACTIVE_WINDOW 2>/dev/null | awk "{print \$2}")" WM_NAME _NET_WM_PID 2>/dev/null || true)
if [ -n "$_XPROP_RESULT" ]; then
WM_NAME=$(echo "$_XPROP_RESULT" | grep -oP '(?<=WM_NAME = ").*(?=")')
_NET_WM_PID=$(echo "$_XPROP_RESULT" | grep -oP '(?<=_NET_WM_PID = )\d+')
else
unset WM_NAME
unset _NET_WM_PID
fi
if [ -z "$_NET_WM_PID" ]; then
WM_EXE="unknown"
WM_CMDLINE="unknown"
else
WM_EXE="$(readlink "/proc/${_NET_WM_PID}/exe")"
WM_CMDLINE="$(tr '\0' ' ' < "/proc/${_NET_WM_PID}/cmdline")"
fi
if [ -z "$HEARTBEATURL" ]; then
echo "Dry run mode, not sending heartbeat."
echo "mac=$MACADDR"
echo "version=$VERSION"
echo "uptime=$UPTIME"
echo "seats=$SEATS"
echo "os=$OS"
echo "kernel=$KERNEL"
echo "cpu=$CPU"
echo "cpuused=$CPUUSED"
echo "mem=$MEM"
echo "memused=$MEMUSED"
echo "load=$LOAD"
echo "window_name=$WM_NAME"
echo "window_exe=$WM_EXE"
echo "window_cmdline=$WM_CMDLINE"
else
REPORT_URL="$HEARTBEATURL"
if [ -n "$REPORTTOKEN" ]; then
case "$REPORT_URL" in
*\?*) REPORT_URL="${REPORT_URL}&token=${REPORTTOKEN}" ;;
*) REPORT_URL="${REPORT_URL}?token=${REPORTTOKEN}" ;;
esac
fi
curl -s "$REPORT_URL" \
--form-string "mac=$MACADDR" \
--form-string "version=$VERSION" \
--form-string "uptime=$UPTIME" \
--form-string "seats=$SEATS" \
--form-string "os=$OS" \
--form-string "kernel=$KERNEL" \
--form-string "cpu=$CPU" \
--form-string "cpuused=$CPUUSED" \
--form-string "mem=$MEM" \
--form-string "memused=$MEMUSED" \
--form-string "load=$LOAD" \
--form-string "wifi_signal=$WIFI_SIGNAL" \
--form-string "wifi_bssid=$WIFI_BSSID" \
--form-string "window_name=$WM_NAME" \
--form-string "window_exe=$WM_EXE" \
--form-string "window_cmdline=$WM_CMDLINE" \
-o /dev/null
fi