[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 23/23] fix config json conflict and variable samples


MIME-Version: 1.0
Content-Transfer-Encoding: 8bit

From: arf20 <aruizfernandez05@xxxxxxxxx>

---
 alert.c             | 14 +++++++-------
 check.c             |  2 +-
 config.c            | 12 +++++++++++-
 config.h            |  2 ++
 monitor.c           | 29 ++++++++++++++++-------------
 monitor.cfg.example |  2 ++
 monitor.h           |  4 +---
 7 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/alert.c b/alert.c
index 2aef5ff..4e6ccb6 100644
--- a/alert.c
+++ b/alert.c
@@ -55,7 +55,7 @@ send_api(const target_t *target, const char *endpoint, const char *content_type,
     list = curl_slist_append(list, buff); /* copies */
     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
 
-    snprintf(buff, 4096, body_tmpl, target->name, status_str[target->status]);
+    snprintf(buff, 4096, body_tmpl, target->name, status_str[target->status[0]]);
     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buff);
 
     CURLcode curl_code = curl_easy_perform(curl);
@@ -127,8 +127,8 @@ send_email(const target_t *target, const char *address,
     strftime(timestr, 256, "%a, %d %b %Y %T %z", tm_now);
 
     snprintf(buff2, 1024, subject_tmpl, target->name,
-        status_str[target->status]);
-    snprintf(buff3, 1024, body_tmpl, target->name, status_str[target->status]);
+        status_str[target->status[0]]);
+    snprintf(buff3, 1024, body_tmpl, target->name, status_str[target->status[0]]);
     snprintf(buff, 4096, "Date: %s\r\nTo: %s\r\nFrom: %s\r\n"
         "Subject: %s\r\n\r\n%s\r\n", timestr, address, alert_config.from,
         buff2, buff3);
@@ -181,10 +181,10 @@ alert_init()
         if (*line == '\n' || *line == '\0')
             continue;
 
-        char *type = strtok(line, ",");
-        char *target = strtok(NULL, ",");
-        char *extra = strtok(NULL, ",");
-        char *body_tmpl = strtok(NULL, ",");
+        char *type = strtok(line, ";");
+        char *target = strtok(NULL, ";");
+        char *extra = strtok(NULL, ";");
+        char *body_tmpl = strtok(NULL, ";");
 
         if (!type || !target) {
             fprintf(stderr, "malformed config line: %s\n", line);
diff --git a/check.c b/check.c
index 2bd599d..4787a35 100644
--- a/check.c
+++ b/check.c
@@ -113,7 +113,7 @@ check_perform(target_t *targets, size_t targets_n)
     for (size_t i = 0; i < targets_n; i++) {
         printf("[%s] [monitor] check #%ld %s: ",
             timestr, check_num, targets[i].name);
-        targets[i].status = check_funcs[targets[i].type](targets[i].target);
+        targets[i].status[0] = check_funcs[targets[i].type](targets[i].target);
     }
 
     check_num++;
diff --git a/config.c b/config.c
index b5b283b..f943c14 100644
--- a/config.c
+++ b/config.c
@@ -8,7 +8,10 @@
 unsigned short port = 0;
 char *tmpl_path = NULL;
 char *log_path = NULL;
-monitor_config_t monitor_config = { .interval = DEFAULT_INTERVAL };
+monitor_config_t monitor_config = {
+    .interval = DEFAULT_INTERVAL,
+    .samples = DEFAULT_SAMPLES
+};
 alert_config_t   alert_config = { 0 };
 
 int
@@ -61,6 +64,13 @@ config_load(const char *conf_path)
                 fprintf(stderr, "[config] invalid interval: %s\n", line);
                 return -1;
             }
+        } else if (strcmp(line, "samples") == 0) {
+            monitor_config.samples = atoi(value);
+            printf("\tsamples: %d\n", monitor_config.samples);
+            if (monitor_config.samples == 0) {
+                fprintf(stderr, "[config] invalid samples: %s\n", line);
+                return -1;
+            }
         } else if (strcmp(line, "template") == 0) {
             value[strlen(value) - 1] = '\0';
             tmpl_path = strdup(value);
diff --git a/config.h b/config.h
index 4801393..3625bd7 100644
--- a/config.h
+++ b/config.h
@@ -10,12 +10,14 @@
 
 #define DEFAULT_PORT        8888
 #define DEFAULT_INTERVAL    60
+#define DEFAULT_SAMPLES     60
 #define DEFAULT_TMPL_PATH   "index.htm.tmpl"
 #define DEFAULT_LOG_PATH    "events.log"
 
 /* config types */
 typedef struct {
     time_t interval;
+    int samples;
     char *target_config;
 } monitor_config_t;
 
diff --git a/monitor.c b/monitor.c
index 3cae6d0..8e75d86 100644
--- a/monitor.c
+++ b/monitor.c
@@ -212,9 +212,9 @@ monitor_init()
         if (*line == '\n' || *line == '\0')
             continue;
 
-        char *type = strtok(line, ",");
-        char *name = strtok(NULL, ",");
-        char *target = strtok(NULL, ",");
+        char *type = strtok(line, ";");
+        char *name = strtok(NULL, ";");
+        char *target = strtok(NULL, ";");
 
         if (!type || !name || !target) {
             fprintf(stderr, "malformed config line: %s\n", line);
@@ -236,6 +236,9 @@ monitor_init()
         targets[targets_size].target = strdup(target);
         targets[targets_size].status = STATUS_DOWN;
 
+        targets[targets_size].status =
+            malloc(sizeof(status_t) * monitor_config.samples);
+
         /* read monitor logs */
         targets[targets_size].events_capacity = INIT_VEC_CAPACITY;
         targets[targets_size].events_size = 0;
@@ -405,7 +408,7 @@ monitor_generate_status_html()
             "<td class=\"w-max\">%s</td></tr>\n",
             type_str[targets[i].type],          /* type */
             targets[i].target,                  /* target */
-            status_html[targets[i].status],     /* status */
+            status_html[targets[i].status[0]],     /* status */
             target_uptime(&targets[i]),         /* uptime */
             255-color_map(perc_month), color_map(perc_month), 100.0f*perc_month,
             255-color_map(perc_total), color_map(perc_total), 100.0f*perc_total,
@@ -469,7 +472,6 @@ commit_event(const char *log_path, const target_t *target,
     fclose(logf);
 }
 
-
 void
 monitor_update_events(const char *log_path)
 {
@@ -479,20 +481,20 @@ monitor_update_events(const char *log_path)
 
     for (size_t i = 0; i < targets_size; i++) {
         if (targets[i].events_size > 0 && (
-            targets[i].status ==
+            targets[i].status[0] ==
             targets[i].events[targets[i].events_size - 1].status))
         {
             continue;
         }
 
-        if (targets[i].status != targets[i].status_1) {
-            targets[i].status_1 = targets[i].status;
-            continue;
+        for (int j = 0; j < monitor_config.samples - 1; j++) {
+            if (targets[i].status[j] != targets[i].status[j + 1])
+                goto unstable;
         }
 
         event_t event = {
             time_now,
-            targets[i].status
+            targets[i].status[0]
         };
 
         target_events_push_ordered(&targets[i], &event);
@@ -500,11 +502,12 @@ monitor_update_events(const char *log_path)
         commit_event(log_path, &targets[i], &event);
 
         printf("[%s] [monitor] %s is now %s\n",
-            timestr, targets[i].name, status_str[targets[i].status]);
+            timestr, targets[i].name, status_str[targets[i].status[0]]);
 
         alert_trigger(&targets[i]);
-
-        targets[i].status_1 = targets[i].status;
+unstable:
+        memmove(&targets[i].status[1], &targets[i].status[0],
+            sizeof(int) * (monitor_config.samples - 1));
     }
 }
 
diff --git a/monitor.cfg.example b/monitor.cfg.example
index b3a9911..83d6cb6 100644
--- a/monitor.cfg.example
+++ b/monitor.cfg.example
@@ -6,6 +6,8 @@ port=8888
 
 # monitor interval in seconds (sleep)
 interval=60
+# verify 5 times
+samples=5
 
 # html template path
 template=index.htm.tmpl
diff --git a/monitor.h b/monitor.h
index f39441c..538b684 100644
--- a/monitor.h
+++ b/monitor.h
@@ -23,9 +23,7 @@ typedef struct {
     mon_type_t type;
     char *name;
     char *target;
-
-    status_t status, status_1;
-
+    status_t *status;
     event_t *events;
     size_t events_size, events_capacity;
 } target_t;
-- 
2.47.3


References:
[arfnet2-status PATCH 00/23] First releasearf20 <arf20@xxxxxxxxx>