-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.tcl
530 lines (464 loc) · 14.1 KB
/
status.tcl
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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package require fa_sysinfo
package require piaware
#
# configure_statics - configure static values that don't change.
#
proc configure_statics {} {
set ::ethernetmac [::fa_sysinfo::mac_address]
set ::wlandev [::fa_sysinfo::wireless_interface]
}
#
# Set the field from piaware-config.txt into the interface
# Call this everytime a change to the config file is made
# Piaware config settings can be accessed using [piawareConfig get name]
# PiAware config settings can be updated with [piawareConfig option name value]
proc load_piaware_config {} {
change_network_status
}
# Set the network fields from piaware-config.txt into the interface
#
proc change_network_status {} {
set ::wiredenable [piawareConfig get wired-network]
set ::wirelessenable [piawareConfig get wireless-network]
#Set wired status
if {[piawareConfig get wired-network]} {
if {[piawareConfig get wired-type] == "dhcp"} {
set ::staticipaddress ""
set ::staticnetmask ""
set ::staticbroadcast ""
set ::staticgateway ""
} elseif {[piawareConfig get wired-type] == "static"} {
set ::staticipaddress [piawareConfig get wired-address]
set ::staticnetmask [piawareConfig get wired-netmask]
set ::staticbroadcast [piawareConfig get wired-broadcast]
set ::staticgateway [piawareConfig get wired-gateway]
} else {
log "Invalid settings in piaware-config.txt for wired-type. Must be dhcp or static."
}
} else {
}
if {[piawareConfig exists wireless-ssid]} {
set ::wifissid [piawareConfig get wireless-ssid]
} else {
set ::wifissid " "
}
if {[piawareConfig exists wireless-password]} {
set ::wifipassword "password set"
} else {
set ::wifipassword "password not set"
}
}
proc change_to_dhcp {} {
toggle networkstatic 0
}
proc change_to_static {} {
toggle networkdhcp 0
}
#
# update_ethernet_status - get the ethernet link status, speed, IP and
# default gateway and setindicators accordingly.
#
proc update_network_status {} {
set ethernetState [::fa_sysinfo::interface_state eth0]
set wifiState [::fa_sysinfo::interface_state $::wlandev]
set ::ipaddress [::fa_sysinfo::interface_ip_address eth0]
set ::wifiaddress [::fa_sysinfo::interface_ip_address $::wlandev]
set ::ethernetspeed [::fa_sysinfo::interface_speed eth0]
set ::wifispeed [::fa_sysinfo::interface_speed $::wlandev]
#Set the icons on the Network screen (wired, builtin_wireless, usb_wireless) and the network on the device status screen
#ethernetState is up if a cable is connected ethernet port. This doesn't mean there is an IP
if {$::ipaddress != ""} {
set_status_icon wired green
set ::ethernetstatus "Ethernet Connected"
} else {
if {[piawareConfig get wired-network]} {
if {$ethernetState == "up"} {
set_status_icon wired yellow
set ::ethernetstatus "Cable connected no IP"
} else {
set_status_icon wired yellow
set ::ethernetstatus "On but not connected"
}
} else {
set_status_icon wired red
set ::ethernetstatus "Ethernet Off"
}
}
if {$wifiState == "up" ? 1 : 0} {
if {[piawareConfig get rfkill]} {
set_status_icon usb_wireless green
set ::wifistatus "USB Wifi Connected"
} else {
set_status_icon builtin_wireless green
set ::wifistatus "Wifi Connected"
}
} else {
set_status_icon builtin_wireless red
set_status_icon usb_wireless red
if {[piawareConfig get wireless-network]} {
set ::wifistatus "On but not connected"
} else {
set ::wifistatus "Wifi OFF"
}
}
#Update the ip address on the wired and wireless screens
#If wired IP address is blank set it to the wireless IP address
if {$::ipaddress != ""} {
set ::ipaddress_mainmenu "$::ipaddressText $::ipaddress"
} else {
set ::ipaddress_mainmenu "$::ipaddressText $::wifiaddress"
}
#Update the network icon on the main screen
if {$ethernetState == "up" && $::ipaddress != "" || $wifiState == "up" && $::wifiaddress != ""} {
set_status_icon_style mainmenu_network green
} else {
set ::urlhint ""
set_status_icon_style mainmenu_network red
}
#Update the map link
#::fa_sysinfo::route_to_flightaware ::defaultgateway iface ip
#if {$::defaultgateway ne ""} {
# set ::dhcpnetmask [::fa_sysinfo::interface_netmask $iface]
#
# if {[status radio] == "statusGreenIcon"} {
# grid .settings.4
# set ::urlhint "To see a map of aircraft positions, go to this link in a web browser: http://$ip/skyaware"
# } elseif {[status uat_radio] == "statusGreenIcon"} {
# grid .settings.4
# set ::urlhint "To see a map of aircraft positions, go to this link in a web browser: http://$ip/skyaware978"
# } else {
# set ::urlhint ""
# grid remove .settings.4
# }
#} else {
# set ::dhcpnetmask ""
# set ::urlhint ""
# grid remove .settings.4
#}
}
#
# periodically_update_ethernet_status - check to see if the Ethernet is
# up or down, etc, every few seconds
#
proc periodically_update_network_status {} {
after 15000 periodically_update_network_status
update_network_status
set temp [::fa_sysinfo::cpu_temperature]
if {$temp ne ""} {
set ::cputemperature [format "%.1fC / %.1fF" $temp [expr {$temp * 1.8 + 32}]]
} else {
set ::cputemperature "unknown"
}
set load [::fa_sysinfo::cpu_load]
if {$load ne ""} {
set ::cpuload "$load%"
} else {
set ::cpuload "unknown"
}
}
proc total_tx_bytes {} {
return [expr {[::fa_sysinfo::interface_tx_bytes eth0] + [::fa_sysinfo::interface_tx_bytes $::wlandev]}]
}
proc total_rx_bytes {} {
return [expr {[::fa_sysinfo::interface_rx_bytes eth0] + [::fa_sysinfo::interface_rx_bytes $::wlandev]}]
}
#
# init_transfer_statistics - initialize Ethernet transfer statistics
#
proc init_transfer_statistics {} {
set ::txbytes [total_tx_bytes]
set ::rxbytes [total_rx_bytes]
}
#
# update_transfer_statistics - update Ethernet trasfer statistics
#
proc update_transfer_statistics {} {
set priorBytesSent $::txbytes
set ::txbytes [total_tx_bytes]
set ::bytessent [comma $::txbytes]
set ::txkbsec [format "%.1f" [expr {($::txbytes - $priorBytesSent) * 8 / (5 * 1000.0)}]]
set priorBytesReceived $::rxbytes
set ::rxbytes [total_rx_bytes]
set ::bytesreceived [comma $::rxbytes]
set ::rxkbsec [format "%.1f" [expr {($::rxbytes - $priorBytesReceived) * 8 / (5 * 1000.0)}]]
}
#
# periodically_update_transfer_statistics - update Ethernet transfer stats
# every few seconds
#
proc periodically_update_transfer_statistics {} {
after 5000 periodically_update_transfer_statistics
update_transfer_statistics
}
#
# Read PiAware status from Json file and update the status
#
proc periodically_update_piaware_status {} {
try {
set interval [update_piaware_status]
} on error {result} {
puts stderr "Caught '$result' reading piaware status file"
set interval 5000
set value [dict create "status" "red" "message" "Couldn't read status file"]
set ::piawareStatus $value
set_status_indicator piaware $value
set_status_indicator mlat $value
set_status_indicator flightaware $value
set_status_indicator network $value
set_status_indicator gps $value
set_status_indicator radio $value
} finally {
after $interval periodically_update_piaware_status
}
}
proc update_piaware_status {} {
set jsondict [read_json "/run/piaware/status.json"]
#Check if valid time has expired
if {[expr {[dict get $jsondict expiry] < [clock milliseconds]}]} {
error "status file is out of date"
}
foreach {name value} $jsondict {
switch $name {
piaware {
set ::piawareStatus $value
set_status_indicator piaware $value
}
mlat {
set_status_indicator mlat $value
}
adept {
set_status_indicator flightaware $value
}
gps {
set_status_indicator gps $value
}
radio {
set_status_indicator radio $value
}
uat_radio {
set_status_indicator uat_radio $value
}
no_radio {
set_status_indicator no_radio $value
}
}
}
# Determine what radio indicator is active; default to 1090
if {[info exists ::indicator_path(uat_radio)]} {
set active_radio uat_radio
} else {
set active_radio radio
}
# Set the status indicator on the main menu depending on the Device status indicators
# if any are red, set main menu Status to red
# if all are green, set main menu Status to green
# else set main menu Status to yellow
if {[status flightaware] == "statusRedIcon" || [status $active_radio] == "statusRedIcon"} {
set_status_icon_style mainmenu_status red
} elseif {[status flightaware] == "statusGreenIcon" && [status $active_radio] == "statusGreenIcon"} {
set_status_icon_style mainmenu_status green
} else {
set_status_icon_style mainmenu_status yellow
}
return [dict get $jsondict interval]
}
# Take a json status message and sets the indicator
proc set_status_indicator {indicator json} {
set status [dict get $json status]
set message [dict get $json message]
switch $status {
red - amber - green {
set_status_icon $indicator $status
}
default {
set_status_icon $indicator red
}
}
if {[info exists ::indicator_path($indicator)]} {
append indicatortext $::indicator_path($indicator) text
$indicatortext configure -text $message
}
}
# Sets the icon for the status indicator (green, yellow, red)
proc set_status_icon {indicator icon} {
switch $icon {
green {
set icon statusGreenIcon
}
yellow - amber {
set icon statusYellowIcon
}
red {
set icon statusRedIcon
}
default {
set icon $icon
}
}
set ::indicators($indicator) $icon
if {[info exists ::indicator_path($indicator)]} {
$::indicator_path($indicator) configure -image $icon
}
}
#set the icon inside of a style indicator
proc set_status_icon_style {indicator icon} {
switch $icon {
green {
set style mainMenuGreen.TButton
}
yellow - amber {
set style mainMenuYellow.TButton
}
red {
set style mainMenuRed.TButton
}
default {
set style mainMenuRed.TButton
}
}
set ::indicators($indicator) $icon
if {[info exists ::indicator_path($indicator)]} {
$::indicator_path($indicator) configure -style $style
}
}
#
# status - update one of the status indicators by name,
# possible states are green (up) / amber (possible problem) / red (down)
#
# if state isn't specified, returns the current status
#
proc status {item {state ""}} {
# if they asked only for the status without specifying the state to set
# it to, return the status and if there isn't a status yet, return red
if {$state == ""} {
if {[info exists ::indicators($item)]} {
return $::indicators($item)
}
return "red"
}
# configure the indicator color accordingly
# and normalize the state
switch $state {
green - 1 {
set state green
set color green3
set icon statusGreenIcon
}
amber {
set state amber
set color gold2
set icon statusYellowIcon
}
red - 0 {
set state red
set color red
set icon statusRedIcon
}
default {
error "unknown status indicator state: $state"
}
}
# update indicator array with item state
set ::indicators($item) $state
if {[info exists ::indicator_path($item)]} {
$::indicator_path($item) configure -image $icon
}
}
proc toggle {item toggle} {
#disable or enable indicator
if {$toggle} {
$::indicator_path($item).$item configure -state normal
$::indicator_path($item).$item configure -background blue
} else {
$::indicator_path($item).$item configure -state disabled
$::indicator_path($item).$item configure -background grey
}
}
# Open a Message Box with the set indicator message
# If no message is set use a default message
proc create_indicator_messagebox {indicator} {
if {![info exist ::indicators($indicator)]} {
if {$::piawareStatus == "red"} {
tk_messageBox -type ok -message "PiAware is not active. Check System logs for more information."
return
}
tk_messageBox -type ok -message "Indicator not active."
return
}
if {![info exist ::indicator_message($indicator)]} {
tk_messageBox -type ok -message "No status message set. Please try again later."
return
}
tk_messageBox -type ok -message "$::indicator_message($indicator)"
}
#
# load_logo - create a photo object image of the FA logo and put it on the
# display
#
proc load_logo {} {
namespace eval ::images {}
image create photo ::images::logo -file FA_logo_CMYK_with_tag_360.gif
label .f.logo.logo -image ::images::logo
pack .f.logo.logo -side left
}
#
# setup_tod - setup a time of day field
#
proc setup_tod {w} {
label $w.tod -textvariable tod -font LabelFont -relief sunken -width 10
pack $w.tod -side right
label $w.todlabel -text "time" -font LabelFont -foreground grey -width 6
pack $w.todlabel -side right
periodically_update_time
}
#
# periodically_update_time - once a minute update the displayed time
#
proc periodically_update_time {} {
set clock [clock seconds]
after [expr {60000 - ($clock % 60) * 1000}] periodically_update_time
set ::tod [clock format $clock -format "%H:%M %Z"]
update_uptime
}
proc setup_uptime {w} {
label $w.uptime -textvariable uptime -font LabelFont -relief sunken -width 16
pack $w.uptime -side right
label $w.uptimelabel -text "up" -font LabelFont -foreground grey -width 6
pack $w.uptimelabel -side right
}
proc update_uptime {} {
set uptime [::fa_sysinfo::uptime]
set uptime_minutes [expr {$uptime / 60}]
set minutes [expr {$uptime_minutes % 60}]
set uptime_hours [expr {$uptime_minutes / 60}]
set hours [expr {$uptime_hours % 24}]
set days [expr {$uptime_hours / 24}]
set uptimeString ""
if {$days > 0} {
if {$days == 1} {
append uptimeString "1 day, "
} else {
append uptimeString "$days days, "
}
}
append uptimeString [format "%d:%02d" $hours $minutes]
set ::uptime $uptimeString
}
# Parse each line of netstat for ports we care about
proc process_netstat_line {line} {
lassign $line proto recvq sendq localAddress foreignAddress state pidProg
set port [lindex [split $foreignAddress :] 1]
if {$pidProg eq "-"} {
set pid "unknown"
set prog "unknown progress"
} else {
lassign [split $pidProg "/"] pid prog
}
switch -glob $prog {
"piaware" {
if {[string match "*:1200" $foreignAddress] && $state == "ESTABLISHED"} {
set ::tcp_port_status green
}
}
}
}