plong

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.inz.fi/plong
Log | Files | Refs

plong.c (1182B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <sys/socket.h>
      4 #include <unistd.h>
      5 
      6 #include "minitls.h"
      7 #include "json.h"
      8 #include "http.h"
      9 #include "cpu.h"
     10 #include "mem.h"
     11 #include "net.h"
     12 
     13 int main(int argc, char **argv)
     14 {
     15 	char buffer[4096];
     16 	struct buf b = { buffer, 1[&buffer] };
     17 	struct buf bjson = b;
     18 	struct buf breply = b;
     19 	struct json j;
     20 	struct tls *ctx;
     21 
     22 	struct cpu c;
     23 	struct mem m;
     24 	struct net n;
     25 
     26 #define E(v) do { int r = v; if (r) { fprintf(stderr, "%s failed: %s\n", #v, strerror(r)); }} while (0)
     27 	E(net_init(&n));
     28 	E(cpu_init(&c));
     29 	E(mem_init(&m));
     30 
     31 	sleep(5);
     32 
     33 	E(json_init(&j, &b));
     34 	E(json_obj_open(&j));
     35 
     36 	E(json_obj_key(&j, "cpu"));
     37 	E(json_obj_open(&j));
     38 	E(cpu_poll(&c, &j));
     39 	E(json_obj_close(&j));
     40 
     41 	E(json_obj_key(&j, "mem"));
     42 	E(json_obj_open(&j));
     43 	E(mem_poll(&m, &j));
     44 	E(json_obj_close(&j));
     45 
     46 	E(json_obj_key(&j, "net"));
     47 	E(json_obj_open(&j));
     48 	E(net_poll(&n, &j));
     49 	E(json_obj_close(&j));
     50 
     51 	E(json_obj_close(&j));
     52 
     53 	E(json_fini(&j, &bjson));
     54 
     55 	ctx = tls_client();
     56 	http_post("inz.fi", "443", "/plop.php", &bjson, &breply, &(int){ AF_UNSPEC }, ctx);
     57 
     58 	fwrite(b.data, breply.data - b.data, 1, stdout);
     59 	puts("");
     60 
     61 	tls_free(ctx);
     62 
     63 	return 0;
     64 }