2024-04-23 23:52:34 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "runtime-config.h"
|
|
|
|
#include <stdio.h>
|
2024-04-24 00:55:06 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2024-04-23 23:52:34 +00:00
|
|
|
#define NOTIFICATION_PREFIX "[h/nest-sanity-checks] "
|
2024-04-24 01:00:23 +00:00
|
|
|
/* No longer URL encoding payloads. TODO: remove later */
|
|
|
|
#pragma GCC poison urlencoded_report
|
2024-04-24 00:55:06 +00:00
|
|
|
/* #define NOTIFICATION_PREFIX_LEN sizeof(NOTIFICATION_PREFIX) */
|
2024-04-23 23:52:34 +00:00
|
|
|
/* notify_init() initializes curl.
|
|
|
|
If this fails, exiting is the only option, as curl must be usable,
|
|
|
|
or alerts will fail to send later on when things break.
|
|
|
|
This file is the only context that should utilize libcurl.
|
|
|
|
Therefore, the curl instance is static. */
|
|
|
|
static CURL *curl;
|
|
|
|
/* notify_init_early is the first thing that runs,
|
|
|
|
* and it makes sure that curl_global_init() does not
|
|
|
|
* run multiple times in parallel somehow.
|
|
|
|
*/
|
|
|
|
int notify_init_early() {
|
|
|
|
CURLcode curl_global_init_res;
|
|
|
|
curl_global_init_res = curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
|
|
if(curl_global_init_res!=CURLE_OK) {
|
|
|
|
fprintf(stderr, "[E] curl_global_init failed.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
2024-04-24 00:55:06 +00:00
|
|
|
}
|
2024-04-23 23:52:34 +00:00
|
|
|
int notify_init() {
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if(curl == NULL) {
|
|
|
|
fprintf(stderr, "[E] curl_easy_init failed.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
CURLcode url_set_result;
|
2024-04-24 00:55:06 +00:00
|
|
|
char *url;
|
|
|
|
url = config_get_url();
|
|
|
|
if(url==NULL) {
|
|
|
|
fprintf(stderr, "[E] notify url is null, exiting!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
url_set_result = curl_easy_setopt(curl, CURLOPT_URL, config_get_url());
|
2024-04-23 23:52:34 +00:00
|
|
|
if(url_set_result!=CURLE_OK) {
|
|
|
|
fprintf(stderr, "[E] Failed to set notify URL. Cannot deliver notifications, exiting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2024-04-24 00:55:06 +00:00
|
|
|
return 0;
|
2024-04-23 23:52:34 +00:00
|
|
|
}
|
2024-04-24 00:55:06 +00:00
|
|
|
const char *build_prefixed_report(const char *report) {
|
2024-04-23 23:52:34 +00:00
|
|
|
/* Adds a prefix to every message that goes to nest admins */
|
|
|
|
/* TODO: do this in a less weird way,
|
|
|
|
* maybe consider creating a C macro that
|
|
|
|
* adds prefixes and calls the report send function */
|
|
|
|
int report_len = strlen(report);
|
2024-04-24 00:55:06 +00:00
|
|
|
int notification_prefix_len = strlen(NOTIFICATION_PREFIX);
|
|
|
|
int post_payload_len = report_len + notification_prefix_len + 1;
|
2024-04-23 23:52:34 +00:00
|
|
|
char *post_payload = malloc(post_payload_len);
|
2024-04-24 00:55:06 +00:00
|
|
|
for(int a=0;a<notification_prefix_len;a++) {
|
2024-04-23 23:52:34 +00:00
|
|
|
post_payload[a] = NOTIFICATION_PREFIX[a];
|
|
|
|
}
|
|
|
|
for(int a=0;a<report_len;a++) {
|
2024-04-24 00:55:06 +00:00
|
|
|
post_payload[notification_prefix_len+a] = report[a];
|
2024-04-23 23:52:34 +00:00
|
|
|
}
|
|
|
|
post_payload[post_payload_len]=0;
|
|
|
|
return post_payload;
|
|
|
|
}
|
|
|
|
int send_alarm(const char *report) {
|
2024-04-24 00:55:06 +00:00
|
|
|
assert(report!=NULL);
|
2024-04-23 23:52:34 +00:00
|
|
|
printf("[I] Sending report '%s'\n", report);
|
2024-04-24 00:55:06 +00:00
|
|
|
const char *prefixed_report;
|
2024-04-23 23:52:34 +00:00
|
|
|
prefixed_report = build_prefixed_report(report);
|
2024-04-24 00:55:06 +00:00
|
|
|
assert(prefixed_report!=NULL);
|
2024-04-23 23:52:34 +00:00
|
|
|
CURLcode post_field_set_result;
|
2024-04-24 01:00:23 +00:00
|
|
|
post_field_set_result = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, prefixed_report);
|
2024-04-23 23:52:34 +00:00
|
|
|
if(post_field_set_result != CURLE_OK) {
|
2024-04-24 01:00:23 +00:00
|
|
|
fprintf(stderr, "[E] Failed to send report, setting CURLOPT_POSTFIELDS to %s failed.\n", prefixed_report);
|
2024-04-23 23:52:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CURLcode res;
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
if(res != CURLE_OK) {
|
2024-04-24 00:55:06 +00:00
|
|
|
fprintf(stderr, "[E] Failed to send report, curl_easy_perform failed: %s\n", curl_easy_strerror(res));
|
2024-04-23 23:52:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|