-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmfcli.go
More file actions
128 lines (98 loc) · 2.94 KB
/
Copy pathmfcli.go
File metadata and controls
128 lines (98 loc) · 2.94 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/gregdel/pushover"
)
const check_dir string = "/tmp/mfchecks/"
const thresholdSecs_Warning int = 70
const thresholdSecs_Critical int = 120
type Settings struct {
App_key string `json:"app_key"`
Recipient_key string `json:"recipient_key"`
}
var settings Settings
func main() {
//jsonFile, err := os.Open("config.json")
jsonFile, err := os.Open("/home/josh/.mf/mfcli.config")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer jsonFile.Close()
// read our opened jsonFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
// we initialize our Users array
//var users Users
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &settings)
app := pushover.New(settings.App_key)
recipient := pushover.NewRecipient(settings.Recipient_key)
newpath := filepath.Join(check_dir)
err = os.MkdirAll(newpath, os.ModePerm)
// https://stackoverflow.com/questions/14668850/list-directory-in-go
files, err := ioutil.ReadDir(check_dir)
if err != nil {
log.Fatal(err)
}
for _, filename := range files {
secsCheck, err := readFile(check_dir + filename.Name())
if err != nil {
panic(err)
}
//var secsNow, ago uint64
var secsNow, agoSecs int
now := time.Now() // current local time
secsNow = int(now.Unix())
agoSecs = secsNow - secsCheck[0]
//fmt.Println(filename.Name() + " - " + string(secsNow) + " - " + string(secsCheck[0]) + " - " + string(ago))
fmt.Printf("%s - %d - %d secs (%d minutes)\n", filename.Name(), secsNow, agoSecs, agoSecs/60)
// TODO: implement a critical time period too.
if agoSecs > thresholdSecs_Warning {
// TODO: check when the last notification was sent
// TODO: have a duration of wait time between notifications
s := strings.Split(filename.Name(), ".")
fmt.Printf("******* WARNING: %s *******\n", s[0])
msg := "WARNING: " + s[0] + " has not checked in"
message := pushover.NewMessage(msg)
// Send the message to the recipient
_, err := app.SendMessage(message, recipient)
if err != nil {
log.Panic(err)
}
// TODO: keep track of when the last notification was sent
}
// TODO: implement a recovery alert when it goes from bad to good
}
}
func readFile(fname string) (nums []int, err error) {
b, err := ioutil.ReadFile(fname)
if err != nil {
return nil, err
}
lines := strings.Split(string(b), "\n")
// Assign cap to avoid resize on every append.
nums = make([]int, 0, len(lines))
for _, l := range lines {
// Empty line occurs at the end of the file when we use Split.
if len(l) == 0 {
continue
}
// Atoi better suits the job when we know exactly what we're dealing
// with. Scanf is the more general option.
n, err := strconv.Atoi(l)
if err != nil {
return nil, err
}
nums = append(nums, n)
}
return nums, nil
}