json config file
This commit is contained in:
parent
8fae4c40a7
commit
e02f15a212
14
sim/sim.py
14
sim/sim.py
@ -5,6 +5,7 @@ import os
|
||||
from time import sleep
|
||||
import argparse
|
||||
|
||||
from random import random
|
||||
|
||||
DEST = "127.0.0.1"
|
||||
|
||||
@ -13,16 +14,25 @@ def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("file")
|
||||
parser.add_argument("port", type=int, default=4200)
|
||||
parser.add_argument("rate", type=float, default=1)
|
||||
parser.add_argument("rate", default=1)
|
||||
args = parser.parse_args()
|
||||
|
||||
sleep_times = [float(i) for i in args.rate.split("-")]
|
||||
|
||||
def sleep_lenght():
|
||||
if len(sleep_times) == 2:
|
||||
return (max(sleep_times) - min(sleep_times)) * random() + min(sleep_times)
|
||||
else:
|
||||
return sleep_times[0]
|
||||
|
||||
with open(os.path.join(os.path.dirname(args.file), args.file), "r") as f:
|
||||
lines = [line.rstrip().encode("UTF-8") for line in f]
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
while True:
|
||||
for line in lines:
|
||||
sock.sendto(line, (DEST, args.port))
|
||||
sleep(args.rate)
|
||||
print(sleep_lenght())
|
||||
sleep(sleep_lenght())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -5,7 +5,7 @@ LDFLAGS = -L $(LIBPATH)
|
||||
LDLIBS = -ljson-c -lcurl -lpthread -lGeoIP
|
||||
LDFLAGS += $(LDLIBS)
|
||||
CFLAGS_STATIC = $(CFLAGS) --static
|
||||
OBJ=helpers.o pfparser.o sysparser.o msgbuffer.o geo.o elasticsearch.o server.o
|
||||
OBJ=helpers.o pfparser.o sysparser.o msgbuffer.o geo.o elasticsearch.o server.o config.o
|
||||
TESTS=$(patsubst %.c,%.test,$(wildcard tests/*.c))
|
||||
|
||||
|
||||
|
77
src/config.c
Normal file
77
src/config.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <errno.h>
|
||||
#include <json-c/json.h>
|
||||
#include "helpers.h"
|
||||
#include "config.h"
|
||||
#include "elasticsearch.h"
|
||||
|
||||
|
||||
char* get_strfield(json_object* obj, char* key) {
|
||||
json_object *o_value = json_object_object_get(obj, key);
|
||||
if(o_value == NULL)
|
||||
return NULL;
|
||||
const char* value = json_object_get_string(o_value);
|
||||
if(value == NULL)
|
||||
return NULL;
|
||||
char* dest = malloc(strlen(value) + 1);
|
||||
strcpy(dest, value);
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
int get_intfield(json_object* obj, char* key) {
|
||||
json_object *o_value = json_object_object_get(obj, key);
|
||||
if(o_value == NULL)
|
||||
return 0;
|
||||
return json_object_get_int(o_value);
|
||||
}
|
||||
|
||||
|
||||
struct config* config_load(char* conf_path) {
|
||||
int fd;
|
||||
if((fd = open(conf_path, O_RDONLY)) == -1)
|
||||
panic("Config load failed");
|
||||
|
||||
struct stat fsize = {0};
|
||||
if(fstat(fd, &fsize) != 0)
|
||||
die("Conf stat failed");
|
||||
|
||||
if(fsize.st_size == 0)
|
||||
die("Empty config");
|
||||
|
||||
char* confdata = mmap(NULL, fsize.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if(confdata == MAP_FAILED)
|
||||
panic("Failed to mmap conf");
|
||||
|
||||
json_object *j_conf;
|
||||
if((j_conf = json_tokener_parse(confdata)) == NULL)
|
||||
die("Json parse failed");
|
||||
|
||||
struct config* conf = calloc(1, sizeof(struct config));
|
||||
|
||||
if((conf->url = get_strfield(j_conf, "elasticsearch")) == NULL)
|
||||
die("Config missing or invalid elasticsearch url");
|
||||
|
||||
if((conf->port = get_intfield(j_conf, "serverport")) == 0)
|
||||
die("Config missing or invalid serverport number");
|
||||
|
||||
json_object_put(j_conf);
|
||||
|
||||
if(munmap(confdata, fsize.st_size) != 0)
|
||||
panic("munmap failed");
|
||||
|
||||
close(fd);
|
||||
return conf;
|
||||
}
|
||||
|
||||
|
||||
void config_free(struct config* conf) {
|
||||
free(conf->url);
|
||||
free(conf);
|
||||
}
|
9
src/config.h
Normal file
9
src/config.h
Normal file
@ -0,0 +1,9 @@
|
||||
struct config {
|
||||
char* url;
|
||||
int port;
|
||||
};
|
||||
|
||||
|
||||
struct config* config_load(char* conf_path);
|
||||
|
||||
void config_free(struct config* conf);
|
4
src/config.json
Normal file
4
src/config.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"elasticsearch": "http://127.0.0.1:8299",
|
||||
"serverport": 4200
|
||||
}
|
@ -1 +1 @@
|
||||
gdb -ex=r --args ./csyslog 4200
|
||||
gdb -ex=r --args ./csyslog config.json
|
||||
|
@ -11,7 +11,7 @@ void geo_init() {
|
||||
gi = GeoIP_open("GeoLiteCity.dat", GEOIP_INDEX_CACHE);
|
||||
gi6 = GeoIP_open("GeoLiteCityv6.dat", GEOIP_INDEX_CACHE);
|
||||
if (gi == NULL || gi6 == NULL) {
|
||||
fprintf(stderr, "Error opening geoip databases\n");
|
||||
fprintf(stderr, "Fatal: could not open geoip databases\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
@ -6,3 +6,8 @@ void panic(const char* s) {
|
||||
perror(s);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void die(const char* s) {
|
||||
fprintf(stderr, "%s\n", s);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
@ -7,3 +7,5 @@ http://www.decompile.com/cpp/faq/file_and_line_error_string.htm*/
|
||||
#define STR(x) _STR(x)
|
||||
|
||||
void panic(const char* s);
|
||||
|
||||
void die(const char* s);
|
||||
|
18
src/main.c
18
src/main.c
@ -2,17 +2,19 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include "config.h"
|
||||
#include "server.h"
|
||||
#include "helpers.h"
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "usage: %s <port>\n", argv[0]);
|
||||
fprintf(stderr, "usage: %s <config.json>\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*Parse port number to integer*/
|
||||
char* portend;
|
||||
/*char* portend;
|
||||
unsigned int portl;
|
||||
portl = strtol(argv[1], &portend, 10);
|
||||
if (portend == NULL || portend == argv[1]) {
|
||||
@ -20,8 +22,16 @@ int main(int argc, char** argv) {
|
||||
exit(1);
|
||||
}
|
||||
assert(portl < USHRT_MAX);
|
||||
unsigned short port = (unsigned short)portl;
|
||||
unsigned short port = (unsigned short)portl;*/
|
||||
|
||||
struct config* conf = config_load(argv[1]);
|
||||
|
||||
printf("url: %s\n", conf->url);
|
||||
printf("port: %d\n", conf->port);
|
||||
|
||||
run_server(conf->port, conf->url);
|
||||
|
||||
config_free(conf);
|
||||
|
||||
run_server(port);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
10
src/server.c
10
src/server.c
@ -29,6 +29,8 @@ pthread_mutex_t buflock;
|
||||
time_t cur_t = {0};
|
||||
struct tm cur_time = {0};
|
||||
|
||||
char* es_url = NULL;
|
||||
|
||||
|
||||
void sig_handler(int signum) {
|
||||
printf("\nExiting on signal %s\n", strsignal(signum));
|
||||
@ -39,7 +41,7 @@ void sig_handler(int signum) {
|
||||
|
||||
|
||||
int submit_events(char* message) {
|
||||
if(put_events(message, "http://192.168.1.120:8298") == 0) {
|
||||
if(put_events(message, es_url) == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
printf("Failed to post messages!\n");
|
||||
@ -214,11 +216,13 @@ int handle_message(char* msg) {
|
||||
|
||||
|
||||
/*UDP server bits mostly lifted from https://cs.nyu.edu/~mwalfish/classes/16sp/classnotes/handout01.pdf*/
|
||||
int run_server(int port) {
|
||||
geo_init();
|
||||
int run_server(int port, char* url) {
|
||||
signal(SIGTERM, sig_handler);
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
geo_init();
|
||||
es_url = url;
|
||||
|
||||
/*Create socket*/
|
||||
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
||||
panic("socket");
|
||||
|
@ -1 +1 @@
|
||||
int run_server(int port);
|
||||
int run_server(int port, char* url);
|
||||
|
@ -1 +1 @@
|
||||
valgrind --leak-check=full --track-origins=yes -v ./csyslog 4200
|
||||
valgrind --leak-check=full --track-origins=yes -v ./csyslog config.json
|
||||
|
Loading…
Reference in New Issue
Block a user