-
Notifications
You must be signed in to change notification settings - Fork 12
/
monitoring
137 lines (125 loc) · 6.54 KB
/
monitoring
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
#!/usr/bin/env bash
function grafana_sql() {
mysql -h database -u "$GRAFANA_USER" -D "$GRAFANA_DATABASE" -N -B -e "$1"
}
function scanner_sql() {
export MYSQL_PWD="${SCANNER_PASSWORD}"; mysql -h "${SCANNER_HOST}" -P "${SCANNER_PORT}" --protocol "${MYSQL_CONNECTION_TYPE}" -u "${SCANNER_USER}" -D "${SCANNER_DATABASE}" -N -B -e "$1"
}
function cleanup() {
echo "$(date +'%F %T') | Starting Cleanup... "
while read -r table; do
grafana_sql 'delete from '"$table"' where timestamp < date_sub(UTC_TIMESTAMP, INTERVAL '"$CLEANUP_DAYS"' DAY)'
done < <(grafana_sql 'SHOW TABLES LIKE "%_data"')
}
function check_pokemon () {
if [ $SCANNER_TYPE == "MAD" ]; then
pokemon=$(scanner_sql "SELECT COUNT(*) FROM pokemon WHERE disappear_time > UTC_TIMESTAMP()")
elif [ $SCANNER_TYPE == "RDM" ]; then
pokemon=$(scanner_sql "SELECT COUNT(*) FROM pokemon WHERE expire_timestamp > UNIX_TIMESTAMP()")
fi
echo "$(date +'%F %T') | Current Pokémon: $pokemon"
grafana_sql 'INSERT INTO pokemon_data(timestamp,value) VALUES(now(),"'"$pokemon"'")'
}
function check_iv () {
if [ $SCANNER_TYPE == "MAD" ]; then
iv=$(scanner_sql "SELECT COUNT(*) FROM pokemon WHERE disappear_time > UTC_TIMESTAMP() AND individual_attack IS NOT NULL")
elif [ $SCANNER_TYPE == "RDM" ]; then
iv=$(scanner_sql "SELECT COUNT(*) FROM pokemon WHERE expire_timestamp > UNIX_TIMESTAMP() AND iv IS NOT NULL")
fi
echo "$(date +'%F %T') | Current Pokémon with IV: $iv"
grafana_sql 'INSERT INTO iv_data(timestamp,value) VALUES(now(),"'"$iv"'")'
}
function check_nearby () {
if [ $SCANNER_TYPE == "MAD" ]; then
nearby=$(scanner_sql "SELECT COUNT(*) FROM pokemon WHERE disappear_time > UTC_TIMESTAMP() AND (seen_type='nearby_cell' OR seen_type='nearby_stop')")
elif [ $SCANNER_TYPE == "RDM" ]; then
nearby=$(scanner_sql "SELECT COUNT(*) FROM pokemon WHERE expire_timestamp > UNIX_TIMESTAMP() AND (pokestop_id IS NOT NULL OR cell_id IS NOT NULL)")
fi
echo "$(date +'%F %T') | Current nearby Pokémon: $nearby"
grafana_sql 'INSERT INTO nearby_data(timestamp,value) VALUES(now(),"'"$nearby"'")'
}
function check_quests () {
if [ $SCANNER_TYPE == "MAD" ]; then
quests=$(scanner_sql "SELECT COUNT(*) FROM trs_quest WHERE FROM_UNIXTIME(quest_timestamp) > CURDATE()")
elif [ $SCANNER_TYPE == "RDM" ]; then
quests=$(scanner_sql "SELECT COUNT(*) FROM pokestop WHERE quest_type is not null")
fi
echo "$(date +'%F %T') | Current Quests: $quests"
grafana_sql 'INSERT INTO quest_data(timestamp,value) VALUES(now(),"'"$quests"'")'
}
function check_tr () {
if [ $SCANNER_TYPE == "MAD" ]; then
tr=$(scanner_sql "SELECT COUNT(*) FROM pokestop_incident WHERE incident_expiration > UTC_TIMESTAMP()")
elif [ $SCANNER_TYPE == "RDM" ]; then
tr=$(scanner_sql "SELECT COUNT(*) FROM pokestop WHERE grunt_type is not null")
fi
echo "$(date +'%F %T') | Current Stops with an incident: $tr"
grafana_sql 'INSERT INTO tr_data(timestamp,value) VALUES(now(),"'"$tr"'")'
}
function check_devices () {
echo "$(date +'%F %T') | Checking device status..."
if [ $SCANNER_TYPE == "MAD" ]; then
while read -r device; do
array=($(for i in $device; do echo $i; done))
devicename=$(scanner_sql 'SELECT name FROM settings_device WHERE device_id="'"${array[0]}"'"')
if [[ ${array[1]} -eq 1 ]]; then
currentStatus="0"
echo "$(date +'%F %T') | Device $devicename offline"
else
currentStatus="1"
fi
grafana_sql 'INSERT INTO device_data(timestamp, device, value) VALUES(now(),"'$devicename'","'"$currentStatus"'")'
done < <(scanner_sql 'SELECT device_id, IF(lastProtoDateTime < UTC_TIMESTAMP() - INTERVAL '"$STATUS_DOWN"' SECOND - currentSleepTime, 1, 0) AS offline FROM trs_status')
elif [ $SCANNER_TYPE == "RDM" ]; then
while read -r device; do
lastData=$(scanner_sql 'SELECT last_seen FROM device WHERE uuid="'"$device"'"')
now=$(date '+%s')
if [[ $lastData -lt $((now-${STATUS_DOWN})) ]]; then
currentStatus="0"
echo "$(date +'%F %T') | Device $devicename offline"
else
currentStatus="1"
fi
grafana_sql 'INSERT INTO device_data(timestamp, device, value) VALUES(now(),"'$device'","'"$currentStatus"'")'
done < <(scanner_sql "SELECT uuid FROM device;")
fi
}
function check_raids () {
if [ $SCANNER_TYPE == "MAD" ]; then
raids=$(scanner_sql "SELECT COUNT(*) total, SUM(CASE WHEN level=1 THEN 1 ELSE 0 END) level1, SUM(CASE WHEN level=2 THEN 1 ELSE 0 END) level2, SUM(CASE WHEN level=3 THEN 1 ELSE 0 END) level3, SUM(CASE WHEN level=4 THEN 1 ELSE 0 END) level4, SUM(CASE WHEN level=5 THEN 1 ELSE 0 END) level5, SUM(CASE WHEN level=6 THEN 1 ELSE 0 END) level6 FROM raid WHERE end > UTC_TIMESTAMP()")
elif [ $SCANNER_TYPE == "RDM" ]; then
raids=$(scanner_sql "SELECT COUNT(*) total, SUM(CASE WHEN raid_level=1 THEN 1 ELSE 0 END) level1, SUM(CASE WHEN raid_level=2 THEN 1 ELSE 0 END) level2, SUM(CASE WHEN raid_level=3 THEN 1 ELSE 0 END) level3, SUM(CASE WHEN raid_level=4 THEN 1 ELSE 0 END) level4, SUM(CASE WHEN raid_level=5 THEN 1 ELSE 0 END) level5, SUM(CASE WHEN raid_level=6 THEN 1 ELSE 0 END) level6 FROM gym WHERE raid_end_timestamp > UNIX_TIMESTAMP()")
fi
array=($(for i in $raids; do echo $i; done))
echo "$(date +'%F %T') | Current total Raids: ${array[0]}"
grafana_sql 'INSERT INTO raid_data(timestamp,raids_total,raids_1,raids_2,raids_3,raids_4,raids_5,raids_6) VALUES(now(),'${array[0]}','${array[1]}','${array[2]}','${array[3]}','${array[4]}','${array[5]}','${array[6]}')'
}
function check_spawnpoints () {
if [ $SCANNER_TYPE == "MAD" ]; then
spawnpoints=$(scanner_sql "select count(*) from trs_spawn where calc_endminsec is null and first_detection > DATE_SUB(now(), INTERVAL 24 HOUR);")
elif [ $SCANNER_TYPE == "RDM" ]; then
spawnpoints=$(scanner_sql "select count(*) from spawnpoint where despawn_sec is null and updated < DATE_SUB(now(), INTERVAL 24 HOUR);")
fi
echo "$(date +'%F %T') | Unknown Spanwpoints in the last 24h: $spawnpoints"
grafana_sql 'INSERT INTO spawnpoint_data(timestamp,value) VALUES(now(),"'"$spawnpoints"'")'
}
while(true); do
sleep 30
cleanup
check_pokemon
sleep 5
check_iv
sleep 5
check_nearby
sleep 5
check_quests
sleep 5
check_tr
sleep 5
check_devices
sleep 5
check_raids
sleep 5
check_spawnpoints
sleep 5
done