8
8
"log"
9
9
"net/http"
10
10
"os"
11
+ "strconv"
11
12
"strings"
12
13
"time"
13
14
"unicode/utf8"
@@ -16,13 +17,15 @@ import (
16
17
"github.com/faiface/beep/mp3"
17
18
"github.com/faiface/beep/speaker"
18
19
"github.com/gen2brain/beeep"
19
- "github.com/sparrc/go-ping"
20
20
"github.com/getlantern/systray"
21
+ "github.com/sparrc/go-ping"
21
22
)
22
23
23
24
// Length in seconds between each cycle of checks
24
25
var wait time.Duration = 3
25
26
var checking = false
27
+ var ready = false
28
+ var numOfChecksRan uint32 = 0
26
29
27
30
func main () {
28
31
fmt .Println ("Grif v0.1 - https://github.com/d4rkd0s/grif (c) 2019 d4rkd0s" )
@@ -32,7 +35,6 @@ func main() {
32
35
fmt .Println ("Administrator command prompt achieved" )
33
36
}
34
37
checkAndCreateHostsFile ()
35
-
36
38
// Code to ask amount of delay
37
39
//
38
40
//fmt.Print("Enter number of seconds to wait between checking all hosts: ")
@@ -42,15 +44,7 @@ func main() {
42
44
// log.Fatal(err)
43
45
//}
44
46
//wait = time.Duration(i)
45
-
46
47
systray .Run (onReady , onExit )
47
- for range time .Tick (wait * time .Second ) {
48
- if ! checking {
49
- go checkHosts ()
50
- } else {
51
- fmt .Println ("Busy checking hosts, waiting another cycle" )
52
- }
53
- }
54
48
}
55
49
56
50
func onReady () {
@@ -67,6 +61,21 @@ func onReady() {
67
61
}
68
62
}
69
63
}()
64
+
65
+ go func () {
66
+ for range time .Tick (wait * time .Second ) {
67
+ if ! ready {
68
+ go notify ("Grif is now running" )
69
+ bark ()
70
+ }
71
+ ready = true
72
+ if ! checking {
73
+ go checkHosts ()
74
+ } else {
75
+ fmt .Println ("Busy checking hosts, waiting another cycle" )
76
+ }
77
+ }
78
+ }()
70
79
}
71
80
72
81
func onExit () {
@@ -90,6 +99,7 @@ func checkHosts() {
90
99
log .Fatal (err )
91
100
}
92
101
for _ , host := range hosts {
102
+ numOfChecksRan ++
93
103
if strings .Contains (host , "http://" ) || strings .Contains (host , "https://" ) {
94
104
checkHTTP (host )
95
105
}
@@ -98,6 +108,7 @@ func checkHosts() {
98
108
// checkICMP(host)
99
109
}
100
110
}
111
+ systray .SetTooltip (fmt .Sprintf ("%s checks ran, %d hosts up, %d hosts down" , strconv .Itoa (int (numOfChecksRan )), getCountOfUpHosts (), getCountOfDownHosts ()))
101
112
fmt .Println ("Done checking hosts" )
102
113
checking = false
103
114
}
@@ -120,6 +131,34 @@ func getHosts() ([]string, error) {
120
131
return lines , scanner .Err ()
121
132
}
122
133
134
+ func getCountOfUpHosts () uint32 {
135
+ var numOfUpHosts uint32 = 0
136
+ var hosts , err = getHosts ()
137
+ if err != nil {
138
+ log .Fatal (err )
139
+ }
140
+ for _ , host := range hosts {
141
+ if ! strings .HasPrefix (host , "#" ) {
142
+ numOfUpHosts ++
143
+ }
144
+ }
145
+ return numOfUpHosts
146
+ }
147
+
148
+ func getCountOfDownHosts () uint32 {
149
+ var numOfDownHosts uint32 = 0
150
+ var hosts , err = getHosts ()
151
+ if err != nil {
152
+ log .Fatal (err )
153
+ }
154
+ for _ , host := range hosts {
155
+ if strings .HasPrefix (host , "#" ) {
156
+ numOfDownHosts ++
157
+ }
158
+ }
159
+ return numOfDownHosts
160
+ }
161
+
123
162
func checkIfHostWasUp (host string ) bool {
124
163
hosts , err := getHosts ()
125
164
if err != nil {
@@ -177,6 +216,17 @@ func markHostUp(host string) {
177
216
return
178
217
}
179
218
219
+ func notify (message string ) {
220
+ // If the host is commented out, don't alert
221
+ err := beeep .Notify ("Grif" , message , "assets/grif.png" )
222
+ if err != nil {
223
+ panic (err )
224
+ }
225
+ // Wait 2 seconds before continuing to let the notifications pool up slowly
226
+ time .Sleep (2 * time .Second )
227
+ return
228
+ }
229
+
180
230
func alert (message string , host string ) {
181
231
// If the host is commented out, don't alert
182
232
if checkIfHostWasUp (host ) {
@@ -186,6 +236,8 @@ func alert(message string, host string) {
186
236
}
187
237
bark ()
188
238
markHostDown (host )
239
+ // TODO: Add downed hosts to list in right click menu
240
+ // If a user clicked a downed host, Grif will recheck (ie remove #)
189
241
}
190
242
// Wait 2 seconds before continuing to let the notifications pool up slowly
191
243
time .Sleep (2 * time .Second )
0 commit comments