summaryrefslogtreecommitdiff
path: root/netmon.c
blob: aa478e62bd551a77323325e5dad6ebc8fa23a45a (plain)
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
#include "logging.h"
#include "netcheck.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

#define OPTPARSE_IMPLEMENTATION
#define OPTPARSE_API static

#include "optparse.h"

const char *logfile = "netmon.log";

// which host to ping. If NULL, test tcp instead
const char *pingdest = NULL;

// TODO support blanks
// cmd to be executed. If NULL, reboot
const char *failcmd = "reboot";

// seconds to sleep between checks
unsigned int check_interval_seconds = 30;

// how many failures to reboot the system
int max_check_failure = 5;

// should run as a daemon process
int as_daemon = 0;

// TODO make failure_sleep_seconds configurable
// seconds to sleep before resuming check after a network failure is detected
unsigned int failure_sleep_seconds = 60;

volatile int failure_detected = 0;

void *logger = NULL;

void daemonize() {
    pid_t pid = 0;
    pid_t sid = 0;
    pid = fork();
    if (pid < 0) {
        perror("fork()");
        log_error(logger, "fork() failed.");
        exit(1);
    }
    if (pid > 0) {
        char buf[32];
        snprintf(buf, 31, "Child process: %d", pid);
        log_info(logger, buf);
        exit(0); // exit parent process
    }
    // unmask the file mode
    umask(0);
    // set new session
    sid = setsid();
    if (sid < 0) {
        perror("setsid()");
        log_error(logger, "setsid() failed.");
        exit(1);
    }
    if (chdir("/")) {
        perror("chdir()");
        log_error(logger, "chdir() failed,");
        exit(1);
    }
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
}

///**
// * Read and escape a string, then write to another buffer, with length limit.
// * @param si
// * @param so
// * @param maxlen
// */
//void escape(const char *si, char *so, size_t maxlen) {
//    size_t wb = 0; // bytes written
//    char c[4] = {'\0'};
//    while ((c[0] = *si++) != '\0') {
//        const char *s;
//        switch (c[0]) {
//            case '\r':
//                s = "\r";
//                break;
//            case '\n':
//                s = "\n";
//                break;
//            case '\t':
//                s = "\t";
//                break;
//            case '\v':
//                s = "\v";
//                break;
//            case '\b':
//                s = "\b";
//                break;
//            case '\a':
//                s = "\a";
//                break;
//            case '\f':
//                s = "\f";
//                break;
//            default:
//                s = c;
//                break;
//        }
//        strcpy(so, s)
//    }
//}

void loop() {
    // check network and sleep
    // if fails too many times continuously, set require_reboot flag and return
    int failures = 0;
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EndlessLoop"
    while (1) {
        log_info(logger, "Check network.");
        if ((pingdest == NULL) ?
            (check_tcp(logger) != 0) :
            (check_ping(logger, pingdest, NULL) != 0)) {
            ++failures;
            char buf[64];
            snprintf(buf, 63, "Network failure detected. counter=%d", failures);
            log_info(logger, buf);
        } else {
            log_info(logger, "Network is OK.");
            failures = 0;
        }
        if (failures > max_check_failure) {
            log_info(logger, "Max failure times exceeded.");
            failure_detected = 1;
            failures = 0; // reset failure counter

            // handle a network failure event
            char tmp[256];
            snprintf(tmp, 255, "Run system command `%s`.", failcmd);
            log_info(logger, tmp);
            system(failcmd);

            snprintf(tmp, 255, "Wait %d secs before resume checking.\n",
                     failure_sleep_seconds);
            log_debug(logger, tmp);
            unsigned t = failure_sleep_seconds;
            while ((t = sleep(t)));

            continue; // resume checking right now
        }
        log_info(logger, "Sleeping...");
        unsigned int t = check_interval_seconds;
        while ((t = sleep(t)));
    }
#pragma clang diagnostic pop
}

int main(int argc, char *argv[]) {
    struct optparse_long opts[] = {
            {"interval",    't', OPTPARSE_REQUIRED},
            {"max-failure", 'n', OPTPARSE_REQUIRED},
            {"log",         'l', OPTPARSE_REQUIRED},
            {"ping",        'p', OPTPARSE_REQUIRED},
            {"command",     'c', OPTPARSE_REQUIRED},
            {"daemon",      'd', OPTPARSE_NONE},
            {"help",        'h', OPTPARSE_NONE},
            {0}
    };
    int option;
    struct optparse options;

    char *end;

    optparse_init(&options, argv);
    while ((option = optparse_long(&options, opts, NULL)) != -1) {
        switch (option) {
            case 't':
                check_interval_seconds = (int) strtol(options.optarg, &end, 10);
                if (*end != '\0') {
                    die("Invalid check interval: %s\n", options.optarg);
                }
                if (check_interval_seconds <= 0) {
                    die("Check interval should be positive.\n");
                }
                break;
            case 'n':
                max_check_failure = (int) strtol(options.optarg, &end, 10);
                if (*end != '\0') {
                    die("Invalid max check failure times: %s\n",
                        options.optarg);
                }
                if (max_check_failure < 0) {
                    die("Max check failure times should not be negative.\n");
                }
                break;
            case 'l':
                logfile = strdup(options.optarg);
                break;
            case 'p':
                pingdest = strdup(options.optarg);
                break;
            case 'c':
                failcmd = strdup(options.optarg);
                break;
            case 'd':
                as_daemon = 1;
                break;
            case 'h':
                printf("Usage: %s "
                       "[-t <check_interval>] "
                       "[-n <max_failure>] "
                       "[-l <log_file>] "
                       "[-c <cmd>] "
                       "[-p <ping_host>] "
                       "[-d]\n",
                       argv[0]);
                exit(0);
            default:
                die("%s: %s\n", argv[0], options.errmsg);
        }
    }

    logger = log_init(logfile);
    log_debug(logger, "DEBUG logging is enabled.");
    if (as_daemon) {
        log_info(logger, "Daemonizing...");
        daemonize();
    }
    log_info(logger, "netmon is started.");
    loop();
    log_info(logger, "netmon is stopped.");
    log_free(logger);
    return 0;
}