aboutsummaryrefslogtreecommitdiff
path: root/monitor.c
blob: e86154b44fb37498d69f169d85fc1049141ba3d2 (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>

#include <curl/curl.h>

#include "monitor.h"

typedef enum {
    TYPE_REACH,
    TYPE_DNS,
    TYPE_WEB
} type_t;

const char *type_str[] = { "reach", "dns", "web" };

typedef enum {
    STATUS_DOWN,
    STATUS_UP
} status_t;

typedef struct {
    type_t type;
    char *target;
    status_t status;
} target_t;


static target_t targets[256];
static size_t target_n = 0;

static CURL *curl;

static char timestr[256];

static size_t
write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
    return size * nmemb;
}

int
monitor_init(const char *cfg_file) {
    FILE *cfgf = fopen(cfg_file, "r");
    if (!cfgf) {
        fprintf(stderr, "Error opening config: %s\n", strerror(errno));
        return -1;
    }

    printf("monitor targets:\n");
    
    char line[256];
    while (fgets(line, sizeof(line), cfgf)) {
        if (*line == '#' || *line == '\n')
            continue;

        line[strlen(line) - 1] = '\0'; /* strip \n */

        char *type = line;
        char *target = strchr(line, '=');
        if (!target) {
            fprintf(stderr, "malformed config line: %s\n", line);
            continue;
        }

        *target = '\0';
        target++;

        if (strcmp(type, "reach") == 0)
            targets[target_n].type = TYPE_REACH;
        else if (strcmp(type, "dns") == 0)
            targets[target_n].type = TYPE_DNS;
        else if (strcmp(type, "web") == 0)
            targets[target_n].type = TYPE_WEB;

        targets[target_n].target = strdup(target);
        targets[target_n].status = STATUS_DOWN;

        printf("\t%s: %s\n", type_str[targets[target_n].type],
            targets[target_n].target);
        
        target_n++;
    } 

    fclose(cfgf);

    CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
    if (res) {
        fprintf(stderr, "Error initializing cURL: %s\n",
            curl_easy_strerror(res));
        return -1;
    }

    curl = curl_easy_init();
    if (!curl) {
        fprintf(stderr, "Error allocating cURL handle\n");
        return -1;
    }

    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

    return 0;
}

const char *
monitor_generate_status_html()
{
    static char buff[65535];

    static char *status_html[] = {
        "down",
        "up"
    };
   
    char *pos = buff;

    for (size_t i = 0; i < target_n; i++) {
        pos += snprintf(pos, 65535,
            "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",
            type_str[targets[i].type],
            targets[i].target,
            status_html[targets[i].status]);
    }

    return buff;  
}

static int
check_reach(const char *endpoint)
{
    return STATUS_DOWN;
}

static int
check_dns(const char *endpoint)
{
    return STATUS_DOWN;
}

static int
check_http(const char *endpoint)
{
    curl_easy_setopt(curl, CURLOPT_URL, endpoint);
    CURLcode curl_code = curl_easy_perform(curl);
    if (curl_code != CURLE_OK) {
        printf("[%s] [monitor] check http: %s: curl_easy_perform() failed: %s\n",
            timestr, endpoint, curl_easy_strerror(curl_code));
        return STATUS_DOWN;
    }

    long http_code;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);

    printf("[%s] [monitor] check http: %s: %ld\n",
        timestr, endpoint, http_code);

    return STATUS_UP;
}

void
monitor_check()
{
    time_t time_now = time(NULL);
    struct tm *tm_now = gmtime(&time_now);
    strftime(timestr, 256, "%Y-%m-%d %H-%M-%S", tm_now);

    static const int (*check_funcs[])(const char *) = {
        check_reach,
        check_dns,
        check_http
    };

    for (size_t i = 0; i < target_n; i++)
        targets[i].status = check_funcs[targets[i].type](targets[i].target);
}