This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc.pb
453 lines (434 loc) · 14.7 KB
/
proc.pb
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
Declare.s encDec(string.s,mode.b)
Declare toLog(msg.s)
Declare Die()
Declare.s buildTime(time.i)
Declare settings(mode.b)
Declare createIcons()
Declare createShittyIcons()
Declare notify(alerts.l,msg.s,url.s)
Declare.s getData(url.s)
Declare getStat(dummy)
Declare getCustom(dummy)
Declare buildCustom()
Declare parseCustom()
Declare checkPRTG(resData.s)
Declare menuActions()
Declare buildMenu()
Declare updateMenu()
Declare trayUpdate()
Declare check()
Procedure toLog(msg.s)
Shared enableDebug.b
Protected log.s,logdate.s
If enableDebug
log = GetEnvironmentVariable("HOME") + "/.config/iPRTGn/debug.log"
If OpenFile(0,log,#PB_File_Append)
logdate = FormatDate("[%dd.%mm.%yy %hh:%ii:%ss] ",Date())
WriteStringN(0,logdate + msg)
CloseFile(0)
EndIf
EndIf
Debug msg
EndProcedure
Procedure.s encDec(string.s,mode.b)
If Len(string)
Protected res.s = Space(1024)
If mode = #encode
Base64Encoder(@string,StringByteLength(string),@res,1024)
Else
Base64Decoder(@string,StringByteLength(string),@res,1024)
EndIf
ProcedureReturn(res)
EndIf
EndProcedure
Procedure Die()
Shared statThread.i,customThread.i
If IsThread(statThread) : KillThread(statThread) : EndIf
If IsThread(customThread) : KillThread(customThread) : EndIf
settings(#save)
toLog("exiting")
End 0
EndProcedure
Procedure.s buildTime(time.i)
time/1000
If time => 60
time/60
ProcedureReturn Str(time) + " min ago"
EndIf
ProcedureReturn Str(time) + " sec ago"
EndProcedure
Procedure settings(mode.b)
Shared myhost.s,mylogin.s,mypass.s,myutime.l,shittyicons.b,customSensors.s,notifyMode.b,enableDebug.b
Protected config.s = GetEnvironmentVariable("HOME") + "/.config"
If FileSize(config) <> -2 : CreateDirectory(config) : EndIf
config + "/iPRTGn"
If FileSize(config) <> -2 : CreateDirectory(config) : EndIf
OpenPreferences(config + "/config.ini")
If mode = #load
myhost = ReadPreferenceString("host",#mydefhost)
mylogin = ReadPreferenceString("login","")
mypass = encDec(ReadPreferenceString("pass",""),#decode)
myutime = ReadPreferenceInteger("update_time",#mydefutime)
customSensors = ReadPreferenceString("custom_sensors","")
If ReadPreferenceString("enable_debug","no") = "yes"
enableDebug = #True
Else
enableDebug = #False
EndIf
Select ReadPreferenceString("notify_mode","device")
Case "sensor"
notifyMode = #sensor
Case "both"
notifyMode = #both
Default
notifyMode = #device
EndSelect
If ReadPreferenceString("shitty_icons","no") = "yes"
shittyicons = #True
Else
shittyicons = #False
EndIf
Debug myhost + "," + mylogin + "," + mypass + "," + Str(myutime)
Else
myhost = GetGadgetText(#host)
mylogin = GetGadgetText(#login)
mypass = GetGadgetText(#pass)
myutime = Val(GetGadgetText(#utime))
customSensors = GetGadgetText(#custom)
If GetGadgetState(#shittyicons) = #PB_Checkbox_Checked
shittyicons = #True
Else
shittyicons = #False
EndIf
WritePreferenceString("host",myhost)
WritePreferenceString("login",mylogin)
WritePreferenceString("pass",encDec(mypass,#encode))
WritePreferenceInteger("update_time",myutime)
WritePreferenceString("custom_sensors",customSensors)
If enableDebug
WritePreferenceString("enable_debug","yes")
Else
WritePreferenceString("enable_debug","no")
EndIf
Select GetGadgetState(#notifytype)
Case #sensor
notifyMode = #sensor
WritePreferenceString("notify_mode","sensor")
Case #both
notifyMode = #both
WritePreferenceString("notify_mode","both")
Default
notifyMode = #device
WritePreferenceString("notify_mode","device")
EndSelect
If shittyicons
WritePreferenceString("shitty_icons","yes")
Else
WritePreferenceString("shitty_icons","no")
EndIf
EndIf
ClosePreferences()
EndProcedure
Procedure createIcons()
If IsImage(#ok) : FreeImage(#ok) : EndIf
If IsImage(#okconn) : FreeImage(#okconn) : EndIf
If IsImage(#alert) : FreeImage(#alert) : EndIf
CatchImage(#ok,?imgOk)
CatchImage(#okconn,?imgOkConn)
CatchImage(#alert,?imgAlert)
;CocoaMessage(0,ImageID(#ok),"setTemplate:",@"YES")
;CocoaMessage(0,ImageID(#okconn),"setTemplate:",@"YES")
;CocoaMessage(0,ImageID(#alert),"setTemplate:",@"YES")
EndProcedure
Procedure createShittyIcons()
If IsImage(#ok) : FreeImage(#ok) : EndIf
If IsImage(#okconn) : FreeImage(#okconn) : EndIf
If IsImage(#alert) : FreeImage(#alert) : EndIf
CatchImage(#ok,?imgShittyOk)
CatchImage(#okconn,?imgShittyOkConn)
CatchImage(#alert,?imgShittyAlert)
;CocoaMessage(0,ImageID(#ok),"setTemplate:",@"YES")
;CocoaMessage(0,ImageID(#okconn),"setTemplate:",@"YES")
;CocoaMessage(0,ImageID(#alert),"setTemplate:",@"YES")
EndProcedure
Procedure notify(alerts.l,msg.s,url.s)
Shared mydir.s,myhost.s
msg = ReplaceString(msg,#DQUOTE$,"'")
msg = ReplaceString(msg,"[","\[")
msg = ReplaceString(msg,"\","\\")
msg = ReplaceString(msg,"!","\!")
msg = "\" + msg
Protected args.s = "-group iPRTGn -title " + #DQUOTE$ + "Alerts: " + Str(alerts) + #DQUOTE$ + " -message " + #DQUOTE$ + msg + #DQUOTE$ + " -open " + #DQUOTE$ + url + #DQUOTE$
toLog("running notify with args [" + args + "]")
RunProgram(mydir + "iPRTGn.app/Contents/MacOS/terminal-notifier",args,mydir + "iPRTGn.app/")
EndProcedure
Procedure.s getData(url.s)
Protected res.b,resData.s,curl.i,agent.s
curl = curl_easy_init()
agent = #myname + "/" + #myver
If curl
curl_easy_setopt(curl,#CURLOPT_URL,@url)
curl_easy_setopt(curl,#CURLOPT_USERAGENT,@agent)
curl_easy_setopt(curl,#CURLOPT_IPRESOLVE,#CURL_IPRESOLVE_V4)
curl_easy_setopt(curl,#CURLOPT_TIMEOUT,#tTimeout)
curl_easy_setopt(curl,#CURLOPT_WRITEFUNCTION,@curlWriteData())
res.b = curl_easy_perform(curl)
resData.s = curlGetData()
curl_easy_cleanup(curl.i)
ProcedureReturn resData
Else
toLog("can't init curl")
EndIf
EndProcedure
Procedure getStat(dummy)
Shared statData.s,myhost.s,mylogin.s,mypass.s
Protected url.s = "http://" + myhost + "/api/table.json?content=sensors&output=json&columns=sensor,device&filter_status=5&username=" + mylogin + "&password=" + mypass
statData = getData(url)
EndProcedure
Procedure getCustom(dummy)
Shared globalLock.i,myhost.s,mylogin.s,mypass.s,custom.sensor()
LockMutex(globalLock)
ForEach custom()
Protected url.s = "http://" + myhost + "/api/getsensordetails.xml?id=" + custom()\id + "&username=" + mylogin + "&password=" + mypass
custom()\sData = getData(url)
Next
UnlockMutex(globalLock)
EndProcedure
Procedure buildCustom()
Shared globalLock.i,custom.sensor()
LockMutex(globalLock)
ClearList(custom())
Protected i.i,sensor.s,sensors.s = GetGadgetText(#custom)
If Len(sensors)
Protected numOfSensors.b = CountString(GetGadgetText(#custom),",") + 1
If numOfSensors
For i = 1 To numOfSensors
sensor.s = StringField(sensors,i,",")
If Len(sensor)
AddElement(custom())
custom()\id = sensor
EndIf
Next
EndIf
EndIf
UnlockMutex(globalLock)
EndProcedure
Procedure parseCustom()
Shared globalLock.i,custom.sensor()
toLog("parsing custom sensors")
CreateRegularExpression(#name,"<name>\s*<!\[CDATA\[(.*)\]\]>\s*<\/name>")
CreateRegularExpression(#lastvalue,"<lastvalue>\s*<!\[CDATA\[(.*)\]\]>\s*<\/lastvalue>")
LockMutex(globalLock)
ForEach custom()
If ExamineRegularExpression(#name,custom()\sData)
If NextRegularExpressionMatch(#name)
custom()\name = RegularExpressionGroup(#name,1)
EndIf
EndIf
If ExamineRegularExpression(#lastvalue,custom()\sData)
If NextRegularExpressionMatch(#lastvalue)
custom()\lastvalue = RegularExpressionGroup(#lastvalue,1)
EndIf
EndIf
Next
UnlockMutex(globalLock)
FreeRegularExpression(#name)
FreeRegularExpression(#lastvalue)
EndProcedure
Procedure checkPRTG(resData.s)
Shared myhost.s,mylogin.s,mypass.s,wndHidden.b,state.b,alertsCount.l,curIcon.b,curMsg.s,lastSuccessCheck.i,notifyMode.b
Protected msg.s
If FindString(resData,"Unauthorized")
toLog("login/password incorrect, setting state to #sErr")
MessageRequester(#myname,"Login/password is incorrect!")
mylogin = ""
mypass = ""
state = #sErr
HideWindow(#wnd,#False,#PB_Window_ScreenCentered) : wndHidden = #False
ElseIf Len(resData)
If ParseJSON(0,resData,#PB_JSON_NoCase)
curIcon = #ok
NewMap PRTGData()
Protected alerts.alerts
ExtractJSONStructure(JSONValue(0),@alerts.alerts,alerts)
Protected curAlerts = alerts\treesize
Select notifyMode
Case #sensor
toLog("parsing data with #sensor mode")
ForEach alerts\sensors()
msg + alerts\sensors()\sensor + ", "
Next
Case #both
toLog("parsing data with #both mode")
Protected NewMap both.s()
ForEach alerts\sensors()
If Len(both(alerts\sensors()\device))
both(alerts\sensors()\device) + "," + alerts\sensors()\sensor
Else
both(alerts\sensors()\device) = alerts\sensors()\sensor
EndIf
Next
ForEach both()
msg + MapKey(both()) + "[" + both() + "], "
Next
Default
toLog("parsing data with #device mode")
Protected NewList devices.s()
ForEach alerts\sensors()
AddElement(devices())
devices() = alerts\sensors()\device
Next
SortList(devices(),#PB_Sort_Ascending)
Protected prev.s
ForEach devices()
If devices() <> prev
prev = devices()
Else
DeleteElement(devices())
EndIf
Next
ForEach devices()
msg + devices() + ", "
Next
EndSelect
If curAlerts : msg = Left(msg,Len(msg)-2) : EndIf
If curAlerts <> alertsCount And msg <> curMsg
If curAlerts
toLog("sending notify about " + Str(curAlerts) + " alerts")
notify(curAlerts,msg,"http://" + myhost + "/alarms.htm?filter_status=5&filter_status=4&filter_status=10&filter_status=13&filter_status=14")
EndIf
alertsCount = curAlerts
curMsg = msg
EndIf
FreeJSON(0)
SetMenuItemText(#menu,#info,"Alerts: " + Str(curAlerts))
lastSuccessCheck = ElapsedMilliseconds()
Else
toLog("data is not a JSON!")
curIcon = #okconn
ProcedureReturn
EndIf
Else
toLog("curl returned no data!")
curIcon = #okconn
ProcedureReturn
EndIf
EndProcedure
Procedure menuActions()
Shared globalLock.i,myhost.s,wndHidden.b,state.b,custom.sensor()
toLog("clicked menu")
Select EventMenu()
Case #info
RunProgram("open","http://" + myhost + "/alarms.htm?filter_status=5&filter_status=4&filter_status=10&filter_status=13&filter_status=14","")
Case #about
MessageRequester(#myname,"v." + #myver + #CRLF$ + "written by deseven, 2015")
Case #settings
state = #sErr
HideWindow(#wnd,#False,#PB_Window_ScreenCentered) : wndHidden = #False
Case #exit
Die()
Case #enter
If Not wndHidden
settings(#save)
HideWindow(#wnd,#True) : wndHidden = #True
check()
EndIf
Default
LockMutex(globalLock)
If EventMenu() <= ListSize(custom())+1
SelectElement(custom(),EventMenu()-1)
RunProgram("open","http://" + myhost + "/sensor.htm?id=" + custom()\id + "#tab3","")
EndIf
UnlockMutex(globalLock)
EndSelect
EndProcedure
Procedure buildMenu()
Shared globalLock.i,ItemLength.CGFloat,StatusBar.i,StatusItem.i,curIconSet.b,alertsCount.l,custom.sensor()
If Not (StatusBar And StatusItem)
ItemLength.CGFloat = 32
StatusBar.i = CocoaMessage(0,0,"NSStatusBar systemStatusBar")
StatusItem.i = CocoaMessage(0,CocoaMessage(0,StatusBar,"statusItemWithLength:",#NSSquareStatusBarItemLength), "retain")
toLog("registered app for status bar")
EndIf
If IsMenu(#menu) : FreeMenu(#menu) : EndIf
CreatePopupMenu(#menu)
MenuItem(#info,"Alerts: " + Str(alertsCount))
UnbindMenuEvent(#menu,#info,@menuActions())
BindMenuEvent(#menu,#info,@menuActions())
MenuBar()
LockMutex(globalLock)
ForEach custom()
MenuItem(ListIndex(custom())+1,custom()\id + " - updating...")
UnbindMenuEvent(#menu,ListIndex(custom())+1,@menuActions())
BindMenuEvent(#menu,ListIndex(custom())+1,@menuActions())
Next
UnlockMutex(globalLock)
MenuBar()
MenuItem(#about,"About")
UnbindMenuEvent(#menu,#about,@menuActions())
BindMenuEvent(#menu,#about,@menuActions())
MenuItem(#settings,"Settings")
UnbindMenuEvent(#menu,#settings,@menuActions())
BindMenuEvent(#menu,#settings,@menuActions())
MenuItem(#exit,"Exit")
UnbindMenuEvent(#menu,#exit,@menuActions())
BindMenuEvent(#menu,#exit,@menuActions())
UnbindMenuEvent(#menu,#enter,@menuActions())
BindMenuEvent(#menu,#enter,@menuActions())
CocoaMessage(0,StatusItem,"setHighlightMode:",@"YES")
CocoaMessage(0,StatusItem,"setLength:@",@ItemLength)
CocoaMessage(0,StatusItem,"setImage:",ImageID(curIconSet))
CocoaMessage(0,StatusItem,"setMenu:",CocoaMessage(0,MenuID(#menu),"firstObject"))
toLog("binded menu actions")
EndProcedure
Procedure updateMenu()
Shared globalLock.i,custom.sensor()
Protected text.s
LockMutex(globalLock)
ForEach custom()
If Len(custom()\lastvalue)
text = "[" + custom()\lastvalue + "] "
Else
text = "[no data] "
EndIf
If Len(custom()\name)
text + custom()\name
Else
text + custom()\id
EndIf
SetMenuItemText(#menu,ListIndex(custom())+1,text)
Next
UnlockMutex(globalLock)
EndProcedure
Procedure trayUpdate()
Shared curIcon.b,curIconSet.b,alertsCount.l,StatusBar.i,StatusItem.i,shittyicons.b
If alertsCount
If curIcon <> curIconSet
CocoaMessage(0,StatusItem,"setImage:",ImageID(curIcon))
curIconSet = curIcon
Else
CocoaMessage(0,StatusItem,"setImage:",ImageID(#alert))
curIconSet = #alert
EndIf
ElseIf curIcon <> curIconSet
CocoaMessage(0,StatusItem,"setImage:",ImageID(curIcon))
curIconSet = curIcon
EndIf
EndProcedure
Procedure check()
Shared myhost.s,mylogin.s,mypass.s,shittyicons.b,myutime.l,wndHidden.b,state.b,curIconSet.b,lastCheck.i
If Not (Len(myhost) And Len(mylogin) And Len(mypass) And myutime)
state = #sErr
HideWindow(#wnd,#False,#PB_Window_ScreenCentered) : wndHidden = #False
toLog("do not have enough data to perform checks, setting state to #sErr")
Else
state = #sOk
lastCheck = -myutime*1000
toLog("everything seems to be ok, setting state to #sOk")
EndIf
buildCustom()
buildMenu()
toLog("finished building menu and custom sensors list")
If shittyicons : createShittyIcons() : Else : createIcons() : EndIf
curIconSet = -1
EndProcedure