-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiotmonitor.zig
967 lines (817 loc) · 33.8 KB
/
iotmonitor.zig
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
//
// IOT Monitor - monitor and recover state for iot device and software agents
//
// pfreydiere - 2019 - 2022
//
const std = @import("std");
const json = std.json;
const debug = std.debug;
const log = std.log;
const assert = debug.assert;
const mem = std.mem;
const os = std.os;
const io = std.io;
const version = @import("version.zig");
// used for sleep, and other, it may be removed
// to relax libC needs
const c = @cImport({
@cInclude("stdio.h");
@cInclude("unistd.h");
@cInclude("signal.h");
@cInclude("time.h");
@cInclude("string.h");
});
const leveldb = @import("leveldb.zig");
const mqtt = @import("mqttlib.zig");
const processlib = @import("processlib.zig");
const topics = @import("topics.zig");
const toml = @import("toml");
const clap = @import("clap");
// profiling, to check performance on functions
// this is mocked in the build.zig (activate this tracy library)
const tracy = @import("tracy");
const stdoutFile = std.io.getStdOut();
const out = std.fs.File.writer(stdoutFile);
const Verbose = false;
// This structure defines the process informations
// with live agent running, this permit to track the process and
// relaunch it if needed
//
const AdditionalProcessInformation = struct {
// pid is to track the process while running
pid: ?i32 = undefined,
// process identifier attributed by IOTMonitor, to track existing processes
// processIdentifier: []const u8 = "",
exec: []const u8 = "",
// last time the process is restarted
lastRestarted: c.time_t = 0,
// number of time, the process is restarted
restartedCount: u64 = 0,
};
const MonitoringInfo = struct {
// name of the device
name: []const u8 = "",
watchTopics: []const u8,
nextContact: c.time_t,
timeoutValue: u32 = 30,
stateTopics: ?[]const u8 = null,
helloTopic: ?[]const u8 = null,
helloTopicCount: u64 = 0,
allocator: mem.Allocator,
// in case of process informations,
// used to relaunch or not the process, permitting to
// take a process out of the monitoring, and then reintegrate it
enabled: bool = true,
associatedProcessInformation: ?*AdditionalProcessInformation = null,
fn init(allocator: mem.Allocator) !*MonitoringInfo {
const device = try allocator.create(MonitoringInfo);
device.allocator = allocator;
device.stateTopics = null;
device.helloTopic = null;
device.helloTopicCount = 0;
device.timeoutValue = 30;
device.associatedProcessInformation = null;
device.enabled = true;
return device;
}
fn deinit(self: *MonitoringInfo) void {
self.allocator.destroy(self);
}
fn updateNextContact(device: *MonitoringInfo) !void {
_ = c.time(&device.*.nextContact);
device.*.nextContact = device.*.nextContact + @intCast(c_long, device.*.timeoutValue);
}
fn hasExpired(device: *MonitoringInfo) !bool {
var currentTime: c.time_t = undefined;
_ = c.time(¤tTime);
const diff = c.difftime(currentTime, device.*.nextContact);
if (diff > 0) return true;
return false;
}
};
fn stripLastWildCard(watchValue: []const u8) ![]const u8 {
assert(watchValue.len > 0);
if (watchValue[watchValue.len - 1] == '#') {
return watchValue[0 .. watchValue.len - 2];
}
return watchValue;
}
test "test update time" {
var d = MonitoringInfo{
.timeoutValue = 1,
.watchTopics = "",
.nextContact = undefined,
.allocator = undefined,
.helloTopic = undefined,
.stateTopics = undefined,
.associatedProcessInformation = undefined,
};
try d.updateNextContact();
_ = c.sleep(3);
debug.assert(try d.hasExpired());
d.timeoutValue = 20;
try d.updateNextContact();
_ = c.sleep(3);
debug.assert(!try d.hasExpired());
}
pub fn secureZero(s: []u8) void {
var i: u32 = 0;
while (i < s.len) {
s[i] = '\x00';
i = i + 1;
}
}
// parse the device info,
// device must have a watch topics
fn parseDevice(allocator: mem.Allocator, name: []const u8, entry: *toml.Table) !*MonitoringInfo {
const device = try MonitoringInfo.init(allocator);
errdefer device.deinit();
const allocName = try allocator.alloc(u8, name.len + 1);
secureZero(allocName);
std.mem.copy(u8, allocName, name);
device.name = allocName;
if (entry.keys.get("exec")) |exec| {
const execValue = exec.String;
assert(execValue.len > 0);
const execCommand = try allocator.allocSentinel(u8, execValue.len, 0);
mem.copy(u8, execCommand, execValue);
const additionalStructure = try allocator.create(AdditionalProcessInformation);
additionalStructure.exec = execCommand;
additionalStructure.pid = null;
additionalStructure.lastRestarted = 0;
additionalStructure.restartedCount = 0;
device.associatedProcessInformation = additionalStructure;
}
if (entry.keys.get("watchTopics")) |watch| {
// there may have a wildcard at the end
// strip it to compare to the received topic
const watchValue = watch.String;
assert(watchValue.len > 0);
const strippedLastWildCard = try stripLastWildCard(watchValue);
const stopic = try allocator.alloc(u8, strippedLastWildCard.len);
mem.copy(u8, stopic, strippedLastWildCard);
device.watchTopics = stopic;
if (Verbose) {
_ = try out.print("add {s} to device {s} \n", .{ device.name, device.watchTopics });
}
} else {
return error.DEVICE_MUST_HAVE_A_WATCH_TOPIC;
}
if (entry.keys.get("stateTopics")) |statewatch| {
// there may have a wildcard at the end
// strip it to compare to the received topic
const watchValue = statewatch.String;
assert(watchValue.len > 0);
const strippedLastWildCard = try stripLastWildCard(watchValue);
const stopic = try allocator.alloc(u8, strippedLastWildCard.len);
mem.copy(u8, stopic, strippedLastWildCard);
device.stateTopics = stopic;
if (Verbose) {
_ = try out.print("add {s} to device {s} \n", .{ device.name, device.stateTopics });
}
}
if (entry.keys.get("helloTopic")) |hello| {
const helloValue = hello.String;
assert(helloValue.len > 0);
const stopic = try allocator.alloc(u8, helloValue.len);
mem.copy(u8, stopic, helloValue);
device.helloTopic = stopic;
if (Verbose) {
_ = try out.print("hello topic for device {s}\n", .{device.helloTopic});
}
}
if (entry.keys.get("watchTimeOut")) |timeout| {
const timeOutValue = timeout.Integer;
device.timeoutValue = @intCast(u32, timeOutValue);
if (Verbose) {
_ = try out.print("watch timeout for topic for device {s}\n", .{device.helloTopic});
}
}
try device.updateNextContact();
return device;
}
const Config = struct { clientId: []u8, mqttBroker: []u8, user: []u8, password: []u8, clientid: []u8, mqttIotmonitorBaseTopic: []u8 };
const HttpServerConfig = struct { activateHttp: bool = true, listenAddress: []u8, port: u16 = 8079 };
var MqttConfig: *Config = undefined;
var HttpConfig: *HttpServerConfig = undefined;
fn parseTomlConfig(allocator: mem.Allocator, _alldevices: *AllDevices, filename: []const u8) !void {
const t = tracy.trace(@src());
defer t.end();
// getting config parameters
var parser: toml.Parser = undefined;
defer parser.deinit();
var config = try toml.parseFile(allocator, filename, &parser); // no custom parser
defer config.deinit();
var it = config.keys.iterator();
while (it.next()) |e| {
// get table value
switch (e.value_ptr.*) {
toml.Value.Table => |table| {
if (table.name.len >= 7) {
const DEVICEPREFIX = "device_";
const AGENTPREFIX = "agent_";
const isDevice = mem.eql(u8, table.name.ptr[0..DEVICEPREFIX.len], DEVICEPREFIX);
const isAgent = mem.eql(u8, table.name.ptr[0..AGENTPREFIX.len], AGENTPREFIX);
if (isDevice or isAgent) {
if (Verbose) {
try out.print("device found :{s}\n", .{table.name});
}
var prefixlen = AGENTPREFIX.len;
if (isDevice) prefixlen = DEVICEPREFIX.len;
const dev = try parseDevice(allocator, table.name.ptr[prefixlen..table.name.len], table);
if (Verbose) {
try out.print("add {s} to device list, with watch {s} and state {s} \n", .{ dev.name, dev.watchTopics, dev.stateTopics });
}
_ = try _alldevices.put(dev.name, dev);
} else {
try out.print("bad prefix for section :{s} , only device_ or agent_ accepted, skipped \n", .{e.key_ptr});
}
}
},
toml.Value.None, toml.Value.String, toml.Value.Boolean, toml.Value.Integer, toml.Value.Float, toml.Value.Array, toml.Value.ManyTables => continue,
}
}
const conf = try allocator.create(Config);
if (config.keys.get("mqtt")) |mqttconfig| {
if (mqttconfig.Table.keys.get("serverAddress")) |configAdd| {
conf.mqttBroker = try allocator.alloc(u8, configAdd.String.len);
mem.copy(u8, conf.mqttBroker, configAdd.String);
} else {
return error.noKeyServerAddress;
}
if (mqttconfig.Table.keys.get("user")) |u| {
conf.user = try allocator.alloc(u8, u.String.len);
mem.copy(u8, conf.user, u.String);
} else {
return error.ConfigNoUser;
}
if (mqttconfig.Table.keys.get("password")) |p| {
conf.password = try allocator.alloc(u8, p.String.len);
mem.copy(u8, conf.password, p.String);
} else {
return error.ConfigNoPassword;
}
if (mqttconfig.Table.keys.get("clientid")) |cid| {
conf.clientid = try allocator.alloc(u8, cid.String.len);
mem.copy(u8, conf.clientid, cid.String);
try out.print("Using {s} as clientid \n", .{conf.clientid});
} else {
conf.clientid = try allocator.alloc(u8, "iotmonitor".len);
mem.copy(u8, conf.clientid, "iotmonitor");
}
const topicBase = if (mqttconfig.Table.keys.get("baseTopic")) |baseTopic| baseTopic.String else "home/monitoring";
conf.mqttIotmonitorBaseTopic = try allocator.alloc(u8, topicBase.len + 1);
conf.mqttIotmonitorBaseTopic[topicBase.len] = 0;
mem.copy(u8, conf.mqttIotmonitorBaseTopic, topicBase[0..topicBase.len]);
} else {
return error.ConfigNoMqttSection;
}
const httpconf = try allocator.create(HttpServerConfig);
if (config.keys.get("http")) |httpconfig| {
httpconf.*.activateHttp = true;
if (httpconfig.Table.keys.get("bind")) |baddr| {
httpconf.listenAddress = try allocator.alloc(u8, baddr.String.len);
mem.copy(u8, httpconf.listenAddress, baddr.String);
} else {
const localhostip = "127.0.0.1";
httpconf.listenAddress = try allocator.alloc(u8, localhostip.len);
mem.copy(u8, httpconf.listenAddress, localhostip);
}
if (httpconfig.Table.keys.get("port")) |port| {
httpconf.*.port = @intCast(u16, port.Integer);
} else {
httpconf.*.port = 8079;
}
}
HttpConfig = httpconf;
MqttConfig = conf;
}
// MQTT call back to handle the error handling and not
// send the error to C paho library
fn _external_callback(topic: []u8, message: []u8) void {
callback(topic, message) catch {
@panic("error in the callback");
};
}
// MQTT Callback implementation
fn callback(topic: []u8, message: []u8) !void {
const t = tracy.trace(@src());
defer t.end();
// MQTT callback
if (Verbose) {
try out.print("on topic {s}\n", .{topic});
try out.print(" message arrived {s}\n", .{message});
try out.writeAll(topic);
try out.writeAll("\n");
}
// look for all devices
var iterator = alldevices.iterator();
// device loop
while (iterator.next()) |e| {
const deviceInfo = e.value_ptr.*;
if (Verbose) {
if (deviceInfo.stateTopics) |stopic| {
try out.print("evaluate state topic {s} with incoming topic {s} \n", .{ stopic, topic });
}
}
const watchTopic = deviceInfo.watchTopics;
const storeTopic = deviceInfo.stateTopics;
const helloTopic = deviceInfo.helloTopic;
if (storeTopic) |store| {
if (try topics.doesTopicBelongTo(topic, store)) |_| {
// always store topic, even if the monitoring is not enabled
// store sub topic in leveldb
// trigger the refresh for timeout
if (Verbose) {
try out.print("sub topic to store value :{s}, in {s}\n", .{ message, topic });
try out.print("length {}\n", .{topic.len});
}
db.put(topic, message) catch |errStorage| {
log.warn("fail to store message {s} for topic {s}, on database with error {} \n", .{ message, topic, errStorage });
};
}
}
if (helloTopic) |hello| {
if (mem.eql(u8, topic, hello)) {
if (Verbose) {
try out.print("device started, put all state informations \n", .{});
}
// count the number of hello topic
//
//
deviceInfo.helloTopicCount += 1;
// iterate on db, on state topic
const itstorage = try db.iterator();
// itstorage is an allocated pointer
defer globalAllocator.destroy(itstorage);
defer itstorage.deinit();
itstorage.first();
while (itstorage.isValid()) {
var storedTopic = itstorage.iterKey();
if (storedTopic) |storedTopicValue| {
defer globalAllocator.destroy(storedTopicValue);
if (storedTopicValue.len >= topic.len) {
const slice = storedTopicValue.*;
if (mem.eql(u8, slice[0..topic.len], topic[0..])) {
if (deviceInfo.enabled) {
// send the state only if the monitoring is enabled
var stateTopic = itstorage.iterValue();
if (stateTopic) |stateTopicValue| {
if (Verbose) {
try out.print("sending state {s} to topic {s}\n", .{ stateTopic.?.*, slice });
}
defer globalAllocator.destroy(stateTopicValue);
const topicWithSentinel = try globalAllocator.allocSentinel(u8, storedTopicValue.*.len, 0);
defer globalAllocator.free(topicWithSentinel);
mem.copy(u8, topicWithSentinel[0..], storedTopicValue.*);
// resend state
cnx.publish(topicWithSentinel, stateTopicValue.*) catch |errorMqtt| {
log.warn("ERROR {} fail to publish initial state on topic {}", .{ errorMqtt, topicWithSentinel });
try out.print(".. state restoring done, listening mqtt topics\n", .{});
};
}
}
}
}
}
itstorage.next();
}
}
} // hello
if (try topics.doesTopicBelongTo(topic, watchTopic)) |_| {
// trigger the timeout for the iot element
try deviceInfo.updateNextContact();
}
}
if (Verbose) {
try out.print("end of callback \n", .{});
}
}
// global types
const AllDevices = std.StringHashMap(*MonitoringInfo);
const DiskHash = leveldb.LevelDBHashArray(u8, u8);
// global variables
var globalAllocator = std.heap.raw_c_allocator;
var alldevices: AllDevices = undefined;
var db: *DiskHash = undefined;
test "read whole database" {
db = try DiskHash.init(&globalAllocator);
const filename = "iotdb.leveldb";
_ = try db.open(filename);
defer db.close();
const iterator = try db.iterator();
defer globalAllocator.destroy(iterator);
defer iterator.deinit();
log.warn("Dump the iot database \n", .{});
iterator.first();
while (iterator.isValid()) {
const optReadKey = iterator.iterKey();
if (optReadKey) |k| {
defer globalAllocator.destroy(k);
const optReadValue = iterator.iterValue();
if (optReadValue) |v| {
log.warn(" key :{} value: {}\n", .{ k.*, v.* });
defer globalAllocator.destroy(v);
}
}
iterator.next();
}
}
// main connection for subscription
var cnx: *mqtt.MqttCnx = undefined;
var cpt: u32 = 0;
const MAGICPROCSSHEADER = "IOTMONITORMAGIC_";
const MAGIC_BUFFER_SIZE = 16 * 1024;
const LAUNCH_COMMAND_LINE_BUFFER_SIZE = 16 * 1024;
fn launchProcess(monitoringInfo: *MonitoringInfo) !void {
assert(monitoringInfo.associatedProcessInformation != null);
const associatedProcessInformation = monitoringInfo.*.associatedProcessInformation.?.*;
const pid = try os.fork();
if (pid == 0) {
// detach from parent, this permit the process to live independently from
// its parent
_ = c.setsid();
const bufferMagic = try globalAllocator.allocSentinel(u8, MAGIC_BUFFER_SIZE, 0);
defer globalAllocator.free(bufferMagic);
_ = c.sprintf(bufferMagic.ptr, "%s%s", MAGICPROCSSHEADER, monitoringInfo.name.ptr);
const commandLineBuffer = try globalAllocator.allocSentinel(u8, LAUNCH_COMMAND_LINE_BUFFER_SIZE, 0);
defer globalAllocator.free(commandLineBuffer);
const exec = associatedProcessInformation.exec;
_ = c.sprintf(commandLineBuffer.ptr, "echo %s;%s;echo END", bufferMagic.ptr, exec.ptr);
// launch here a bash to have a silent process identification
const argv = [_][]const u8{
"/bin/bash",
"-c",
commandLineBuffer[0..c.strlen(commandLineBuffer)],
};
var m = std.BufMap.init(globalAllocator);
// may add additional information about the process ...
try m.put("IOTMONITORMAGIC", bufferMagic[0..c.strlen(bufferMagic)]);
// execute the process
std.process.execve(globalAllocator, &argv, &m) catch {
unreachable;
};
// if succeeded the process is replaced
} else {
try out.print("process launched, pid : {}\n", .{pid});
monitoringInfo.*.associatedProcessInformation.?.pid = pid;
monitoringInfo.*.associatedProcessInformation.?.restartedCount += 1;
_ = c.time(&monitoringInfo.*.associatedProcessInformation.?.lastRestarted);
// launch mqtt restart information process in monitoring
try publishProcessStarted(monitoringInfo);
}
}
test "test_launch_process" {
globalAllocator = std.heap.c_allocator;
alldevices = AllDevices.init(globalAllocator);
var processInfo = AdditionalProcessInformation{
.exec = "sleep 20",
.pid = undefined,
};
var d = MonitoringInfo{
.timeoutValue = 1,
.name = "MYPROCESS",
.watchTopics = "",
.nextContact = undefined,
.allocator = undefined,
.helloTopic = undefined,
.stateTopics = undefined,
.associatedProcessInformation = &processInfo,
};
// try launchProcess(&d);
// const pid: i32 = d.associatedProcessInformation.?.*.pid.?;
// debug.warn("pid launched : {}\n", .{pid});
try alldevices.put(d.name, &d);
// var p: processlib.ProcessInformation = .{};
// const processFound = try processlib.getProcessInformations(pid, &p);
// assert(processFound);
try processlib.listProcesses(handleCheckAgent);
}
fn handleCheckAgent(processInformation: *processlib.ProcessInformation) void {
// iterate over the devices, to check which device belong to this process
// information
var it = alldevices.iterator();
while (it.next()) |deviceInfo| {
const device = deviceInfo.value_ptr.*;
// not on optional
if (device.associatedProcessInformation) |infos| {
// check if process has the magic Key in the process list
var itCmdLine = processInformation.iterator();
while (itCmdLine.next()) |a| {
if (Verbose) {
out.print("look in {s}\n", .{a}) catch unreachable;
}
const bufferMagic = globalAllocator.allocSentinel(u8, MAGIC_BUFFER_SIZE, 0) catch unreachable;
defer globalAllocator.free(bufferMagic);
_ = c.sprintf(bufferMagic.ptr, "%s%s", MAGICPROCSSHEADER, device.name.ptr);
const p = c.strstr(a.ptr, bufferMagic.ptr);
if (Verbose) {
out.print("found {*}\n", .{p}) catch unreachable;
}
if (p != null) {
// found in arguments, remember the pid
// of the process
infos.*.pid = processInformation.*.pid;
if (Verbose) {
out.print("process {} is monitored pid found\n", .{infos.pid}) catch unreachable;
}
break;
}
if (Verbose) {
out.writeAll("next ..\n") catch unreachable;
}
}
} else {
continue;
}
}
}
fn runAllMissings() !void {
// once all the process have been browsed,
// run all missing processes
var it = alldevices.iterator();
while (it.next()) |deviceinfo| {
const device = deviceinfo.value_ptr.*;
if (device.associatedProcessInformation) |processinfo| {
// this is a process monitored
if (device.enabled) {
if (processinfo.*.pid == null) {
out.print("running ...{s} \n", .{device.name}) catch unreachable;
// no pid associated to the info
//
launchProcess(device) catch {
@panic("fail to run process");
};
}
} else {
// monitoring not enabled on the process
}
}
}
}
fn checkProcessesAndRunMissing() !void {
const t = tracy.trace(@src());
defer t.end();
// RAZ pid infos
var it = alldevices.iterator();
while (it.next()) |deviceInfo| {
const device = deviceInfo.value_ptr.*;
if (device.associatedProcessInformation) |infos| {
infos.pid = null;
}
}
// list all process for wrapping
try processlib.listProcesses(handleCheckAgent);
try runAllMissings();
}
// this function publish a watchdog for the iotmonitor process
// this permit to check if the monitoring is up
fn publishWatchDog() !void {
const t = tracy.trace(@src());
defer t.end();
var topicBufferPayload = try globalAllocator.alloc(u8, 512);
defer globalAllocator.free(topicBufferPayload);
secureZero(topicBufferPayload);
_ = c.sprintf(topicBufferPayload.ptr, "%s/up", MqttConfig.mqttIotmonitorBaseTopic.ptr);
var bufferPayload = try globalAllocator.alloc(u8, 512);
defer globalAllocator.free(bufferPayload);
secureZero(bufferPayload);
cpt = (cpt + 1) % 1_000_000;
_ = c.sprintf(bufferPayload.ptr, "%d", cpt);
const payloadLength = c.strlen(bufferPayload.ptr);
cnx.publish(topicBufferPayload.ptr, bufferPayload[0..payloadLength]) catch {
log.warn("cannot publish watchdog message, will retryi \n", .{});
};
}
fn publishProcessStarted(mi: *MonitoringInfo) !void {
const t = tracy.trace(@src());
defer t.end();
var topicBufferPayload = try globalAllocator.alloc(u8, 512);
defer globalAllocator.free(topicBufferPayload);
secureZero(topicBufferPayload);
_ = c.sprintf(topicBufferPayload.ptr, "%s/startedprocess/%s", MqttConfig.mqttIotmonitorBaseTopic.ptr, mi.name.ptr);
var bufferPayload = try globalAllocator.alloc(u8, 512);
defer globalAllocator.free(bufferPayload);
secureZero(bufferPayload);
_ = c.sprintf(bufferPayload.ptr, "%d", mi.*.associatedProcessInformation.?.lastRestarted);
const payloadLength = c.strlen(bufferPayload.ptr);
cnx.publish(topicBufferPayload.ptr, bufferPayload[0..payloadLength]) catch {
log.warn("cannot publish watchdog message, will retryi \n", .{});
};
}
// this publish on the mqtt broker the process informations
//
fn publishDeviceMonitoringInfos(device: *MonitoringInfo) !void {
var topicBufferPayload = try globalAllocator.alloc(u8, 512);
defer globalAllocator.free(topicBufferPayload);
secureZero(topicBufferPayload);
_ = c.sprintf(topicBufferPayload.ptr, "%s/helloTopicCount/%s", MqttConfig.mqttIotmonitorBaseTopic.ptr, device.*.name.ptr);
var bufferPayload = try globalAllocator.alloc(u8, 512);
defer globalAllocator.free(bufferPayload);
secureZero(bufferPayload);
_ = c.sprintf(bufferPayload.ptr, "%u", device.helloTopicCount);
const payloadLen = c.strlen(bufferPayload.ptr);
cnx.publish(topicBufferPayload.ptr, bufferPayload[0..payloadLen]) catch {
log.warn("cannot publish timeout message for device {} , will retry \n", .{device.name});
};
}
// this function pulish a mqtt message for a device that is not publishing
// it mqtt messages
fn publishDeviceTimeOut(device: *MonitoringInfo) !void {
var topicBufferPayload = try globalAllocator.alloc(u8, 512);
defer globalAllocator.free(topicBufferPayload);
secureZero(topicBufferPayload);
_ = c.sprintf(topicBufferPayload.ptr, "%s/expired/%s", MqttConfig.mqttIotmonitorBaseTopic.ptr, device.*.name.ptr);
var bufferPayload = try globalAllocator.alloc(u8, 512);
defer globalAllocator.free(bufferPayload);
secureZero(bufferPayload);
_ = c.sprintf(bufferPayload.ptr, "%d", device.nextContact);
const payloadLen = c.strlen(bufferPayload.ptr);
cnx.publish(topicBufferPayload.ptr, bufferPayload[0..payloadLen]) catch {
log.warn("cannot publish timeout message for device {} , will retry \n", .{device.name});
};
}
const JSONStatus = struct {
name: []const u8,
enabled: bool = true,
expired: bool,
};
fn indexHandler(req: Request, res: Response) !void {
_ = req;
const t = tracy.trace(@src());
defer t.end();
var iterator = alldevices.iterator();
try res.setType("application/json");
try res.body.writeAll("[");
var hasone = false;
while (iterator.next()) |e| {
const deviceInfo = e.value_ptr.*;
if (hasone) {
try res.body.writeAll(",");
}
const j = JSONStatus{ .name = deviceInfo.name[0 .. deviceInfo.name.len - 1], .enabled = deviceInfo.enabled, .expired = try deviceInfo.hasExpired() };
// create a json response associated
try json.stringify(j, json.StringifyOptions{}, res.body);
hasone = true;
}
try res.body.writeAll("]");
// try res.write("IotMonitor version 0.2.2");
}
const Address = std.net.Address;
const routez = @import("routez");
const Request = routez.Request;
const Response = routez.Response;
const Server = routez.Server;
const Thread = std.Thread;
var server: Server = undefined;
var addr: Address = undefined;
// http server context
const ServerCtx = struct {};
pub fn startServer(context: ServerCtx) void {
_ = context;
server.listen(addr) catch {
@panic("cannot start listening http server");
};
}
// main procedure
pub fn main() !void {
const params = comptime [_]clap.Param(clap.Help){
clap.parseParam("-h, --help Display this help") catch unreachable,
clap.parseParam("-v, --version Display version") catch unreachable,
clap.parseParam("<TOML CONFIG FILE>...") catch unreachable,
};
var diag = clap.Diagnostic{};
var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| {
// Report useful error and exit
diag.report(io.getStdErr().writer(), err) catch {};
return err;
};
defer args.deinit();
if (args.flag("--help")) {
debug.print("\n", .{});
debug.print("start the iotmonitor deamon, usage :\n", .{});
debug.print(" iotmonitor [optional config.toml filepath]\n", .{});
debug.print("\n", .{});
return;
}
if (args.flag("--version")) {
debug.print("{s}", .{version.version});
return;
}
try out.writeAll("IotMonitor start, version ");
try out.writeAll(version.version);
try out.writeAll("\n");
// default
const defaultConfigFile = "config.toml";
var configurationFile = try globalAllocator.alloc(u8, defaultConfigFile.len);
mem.copy(u8, configurationFile, defaultConfigFile);
var arg_index: u32 = 0;
for (args.positionals()) |pos| {
debug.print("{s}\n", .{pos});
debug.print("{}\n", .{pos.len});
if (arg_index == 0) {
globalAllocator.free(configurationFile);
configurationFile = try globalAllocator.alloc(u8, pos.len);
mem.copy(u8, configurationFile, pos);
}
arg_index += 1;
}
try out.writeAll("Reading ");
try out.writeAll(configurationFile);
try out.writeAll(" file\n");
// Friendly error if the file does not exists
var openedtestfile = std.os.open(configurationFile, 0, 0) catch {
try out.writeAll("Cannot open file ");
try out.writeAll(configurationFile);
try out.writeAll("\n");
return;
};
std.os.close(openedtestfile);
alldevices = AllDevices.init(globalAllocator);
try parseTomlConfig(globalAllocator, &alldevices, configurationFile);
try out.writeAll("Opening database\n");
db = try DiskHash.init(&globalAllocator);
const filename = "iotdb.leveldb";
_ = try db.open(filename);
defer db.close();
// connecting to MQTT
var serverAddress: []const u8 = MqttConfig.mqttBroker;
var userName: []const u8 = MqttConfig.user;
var password: []const u8 = MqttConfig.password;
var clientid: []const u8 = MqttConfig.clientid;
try out.writeAll("Connecting to mqtt ..\n");
try out.print(" connecting to \"{s}\" with user \"{s}\" and clientid \"{s}\"\n", .{ serverAddress, userName, clientid });
cnx = try mqtt.MqttCnx.init(&globalAllocator, serverAddress, clientid, userName, password);
if (HttpConfig.activateHttp) {
try out.print("Start embedded http server on port {} \n", .{HttpConfig.*.port});
server = Server.init(
globalAllocator,
.{},
.{routez.all("/", indexHandler)},
);
addr = try Address.parseIp4(HttpConfig.*.listenAddress, HttpConfig.*.port);
const threadConfig: Thread.SpawnConfig = .{};
const threadHandle = try Thread.spawn(threadConfig, startServer, .{.{}});
_ = threadHandle;
try out.print("Http server thread launched\n", .{});
}
try out.print("Checking running monitored processes\n", .{});
try checkProcessesAndRunMissing();
try out.print("Restoring saved states topics ... \n", .{});
// read all elements in database, then redefine the state for all
const it = try db.iterator();
defer globalAllocator.destroy(it);
defer it.deinit();
it.first();
while (it.isValid()) {
const r = it.iterKey();
if (r) |subject| {
defer globalAllocator.destroy(subject);
const v = it.iterValue();
if (v) |value| {
defer globalAllocator.destroy(value);
try out.print("Sending initial stored state {s} to {s}\n", .{ value.*, subject.* });
const topicWithSentinel = try globalAllocator.allocSentinel(u8, subject.*.len, 0);
defer globalAllocator.free(topicWithSentinel);
mem.copy(u8, topicWithSentinel[0..], subject.*);
// if failed, stop the process
cnx.publish(topicWithSentinel, value.*) catch |e| {
log.warn("ERROR {} fail to publish initial state on topic {s}", .{ e, topicWithSentinel });
try out.print(".. State restoring done, listening mqtt topics\n", .{});
};
}
}
it.next();
}
try out.print(".. State restoring done, listening mqtt topics\n", .{});
cnx.callBack = _external_callback;
// register to all, it may be huge, and probably not scaling
_ = try cnx.register("#");
while (true) { // main loop
_ = c.sleep(1); // every 1 seconds
{
// if activated trace this function
const t = tracy.trace(@src());
defer t.end();
// check process that has falled down, and must be restarted
try checkProcessesAndRunMissing();
// watchdog
try publishWatchDog();
var iterator = alldevices.iterator();
while (iterator.next()) |e| {
// publish message
const deviceInfo = e.value_ptr.*;
if (deviceInfo.enabled) {
// if the device is enabled
const hasExpired = try deviceInfo.hasExpired();
if (hasExpired) {
try publishDeviceTimeOut(deviceInfo);
}
try publishDeviceMonitoringInfos(deviceInfo);
}
}
}
}
log.warn("ended", .{});
return;
}