-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-monitor.go
149 lines (120 loc) · 3.91 KB
/
process-monitor.go
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
package main
import (
"flag"
"fmt"
"log"
"os/exec"
"strings"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
cmap "github.com/orcaman/concurrent-map"
"gopkg.in/yaml.v2"
)
var (
watchFile string
configFile string
tgBot *tgbotapi.BotAPI
storage cmap.ConcurrentMap
sessionConfig config
)
func init() {
flag.StringVar(&watchFile, "watch", "watch_list.txt", "path to txt file that contain list of process name separated by newline")
flag.StringVar(&configFile, "config", "config.yaml", "path to process-monitor config file")
flag.Parse()
log.Println("initializing config...")
err := yaml.Unmarshal(readFile(configFile), &sessionConfig)
if err != nil {
log.Fatal("yaml.Unmarshal(readFile) failing", err)
}
tgBot, err = tgbotapi.NewBotAPI(sessionConfig.Notifier.Telegram.Token)
if err != nil {
log.Fatal("bot initialization failing", err)
}
go func() {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := tgBot.GetUpdatesChan(u)
if err != nil {
log.Fatal("GetUpdatesChan failing", err)
}
for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
continue
}
msgContent := fmt.Sprintf("Hi there, this is process-monitor bot, please add `%v` into your config.yaml file, and don't give me message access to this room", update.Message.Chat.ID)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, msgContent)
msg.ParseMode = "Markdown"
msg.ReplyToMessageID = update.Message.MessageID
tgBot.Send(msg)
}
}()
storage = cmap.New()
}
func main() {
// ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers,
// each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns",
// "us" (or "µs"), "ms", "s", "m", "h".
repeatEvery, err := time.ParseDuration(sessionConfig.NotifConfig.RepeatEvery)
if err != nil {
log.Fatal("RepeatEvery input failing", err)
}
var watchProcess func(string)
watchProcess = func(processName string) {
defer func(processName string) {
time.Sleep(1 * time.Second)
go watchProcess(processName)
}(processName)
// process down key
pdk := processName + ":last-down"
// process last notified
pln := processName + ":last-notified"
_, err := exec.Command("pidof", processName).Output()
if err != nil {
firstDownTime := int64(0)
_firstDownTime, exist := storage.Get(pdk)
if exist {
if _firstDownTime.(int64) == int64(0) {
now := time.Now().Unix()
storage.Set(pdk, now)
firstDownTime = now
} else {
firstDownTime = _firstDownTime.(int64)
}
}
if lastReport, exist := storage.Get(pln); exist {
lastReportUnix := lastReport.(float64)
currentUnix := float64(time.Now().Unix())
// useless float
if currentUnix-lastReportUnix > repeatEvery.Seconds() {
broadcastError(sessionConfig.Notifier.Telegram.RoomIds, processName, firstDownTime)
// set :last-notified
storage.Set(pln, float64(time.Now().Unix()))
}
} else {
now := time.Now().Unix()
broadcastError(sessionConfig.Notifier.Telegram.RoomIds, processName, now)
// set :last-notified
storage.Set(pln, float64(now))
}
log.Println(processName, "process died")
} else {
if _firstDownTime, exist := storage.Get(pdk); exist {
if _firstDownTime.(int64) != 0 {
broadcastRunning(sessionConfig.Notifier.Telegram.RoomIds, processName)
}
}
storage.Set(pdk, int64(0))
}
}
// read watched file and split by newline
watchedFileList := string(readFile(watchFile))
pNameArr := strings.Split(watchedFileList, "\n")
// start watcher process
for _, line := range pNameArr {
if len(line) > 0 {
log.Println("Watching", line, "process")
go watchProcess(line)
}
}
<-make(chan bool)
}