Skip to content

Commit 8fe5415

Browse files
committed
Added quality of life improvements to grif
1 parent aebddcf commit 8fe5415

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

grif.go

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"net/http"
1010
"os"
11+
"strconv"
1112
"strings"
1213
"time"
1314
"unicode/utf8"
@@ -16,13 +17,15 @@ import (
1617
"github.com/faiface/beep/mp3"
1718
"github.com/faiface/beep/speaker"
1819
"github.com/gen2brain/beeep"
19-
"github.com/sparrc/go-ping"
2020
"github.com/getlantern/systray"
21+
"github.com/sparrc/go-ping"
2122
)
2223

2324
// Length in seconds between each cycle of checks
2425
var wait time.Duration = 3
2526
var checking = false
27+
var ready = false
28+
var numOfChecksRan uint32 = 0
2629

2730
func main() {
2831
fmt.Println("Grif v0.1 - https://github.com/d4rkd0s/grif (c) 2019 d4rkd0s")
@@ -32,7 +35,6 @@ func main() {
3235
fmt.Println("Administrator command prompt achieved")
3336
}
3437
checkAndCreateHostsFile()
35-
3638
// Code to ask amount of delay
3739
//
3840
//fmt.Print("Enter number of seconds to wait between checking all hosts: ")
@@ -42,15 +44,7 @@ func main() {
4244
// log.Fatal(err)
4345
//}
4446
//wait = time.Duration(i)
45-
4647
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-
}
5448
}
5549

5650
func onReady() {
@@ -67,6 +61,21 @@ func onReady() {
6761
}
6862
}
6963
}()
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+
}()
7079
}
7180

7281
func onExit() {
@@ -90,6 +99,7 @@ func checkHosts() {
9099
log.Fatal(err)
91100
}
92101
for _, host := range hosts {
102+
numOfChecksRan++
93103
if strings.Contains(host, "http://") || strings.Contains(host, "https://") {
94104
checkHTTP(host)
95105
}
@@ -98,6 +108,7 @@ func checkHosts() {
98108
// checkICMP(host)
99109
}
100110
}
111+
systray.SetTooltip(fmt.Sprintf("%s checks ran, %d hosts up, %d hosts down", strconv.Itoa(int(numOfChecksRan)), getCountOfUpHosts(), getCountOfDownHosts()))
101112
fmt.Println("Done checking hosts")
102113
checking = false
103114
}
@@ -120,6 +131,34 @@ func getHosts() ([]string, error) {
120131
return lines, scanner.Err()
121132
}
122133

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+
123162
func checkIfHostWasUp(host string) bool {
124163
hosts, err := getHosts()
125164
if err != nil {
@@ -177,6 +216,17 @@ func markHostUp(host string) {
177216
return
178217
}
179218

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+
180230
func alert(message string, host string) {
181231
// If the host is commented out, don't alert
182232
if checkIfHostWasUp(host) {
@@ -186,6 +236,8 @@ func alert(message string, host string) {
186236
}
187237
bark()
188238
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 #)
189241
}
190242
// Wait 2 seconds before continuing to let the notifications pool up slowly
191243
time.Sleep(2 * time.Second)

0 commit comments

Comments
 (0)