123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <machine/endian.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sysexits.h>
- #include <syslog.h>
- #include <unistd.h>
- #include <rrd.h>
- #include "data.h"
- #include "error.h"
- #include "limits.h"
- #include "monmux.h"
- #include "muxnet.h"
- #include "net.h"
- #include "readconf.h"
- #include "share.h"
- #include "xmalloc.h"
- __BEGIN_DECLS
- void exithandler();
- void huphandler(int);
- void signalhandler(int);
- __END_DECLS
- int flag_hup = 0;
- fd_set fdset;
- int maxfd;
- void
- exithandler(int s) {
- info("received signal %d - quitting", s);
- exit(EX_TEMPFAIL);
- }
- void
- huphandler(int s) {
- info("sighup (%d) received", s);
- flag_hup = 1;
- }
- int
- main(int argc, char *argv[])
- {
- struct monpacket packet;
- struct packedstream ps;
- char *cfgfile;
- char *cfgpath;
- char *stringbuf;
- char *stringptr;
- int maxstringlen;
- struct muxlist mul, newmul;
- struct sourcelist sol, newsol;
- char *arg_ra[4];
- struct stream *stream;
- struct source *source;
- struct mux *mux;
- FILE *f;
- int ch;
- int offset;
- time_t timestamp;
- int churnbuflen;
- SLIST_INIT(&mul);
- SLIST_INIT(&sol);
-
-
- flag_debug = 0;
- flag_daemon = 0;
-
- cfgfile = MONMUX_CONFIG_FILE;
- while ((ch = getopt(argc, argv, "dvf:")) != -1) {
- switch (ch) {
- case 'd':
- flag_debug = 1;
- break;
- case 'f':
- if (optarg && optarg[1] != '/') {
-
- if ((cfgpath = getwd(NULL)) == NULL)
- fatal("could not get working directory");
-
- maxstringlen = strlen(cfgpath) + strlen(optarg) + 1;
- cfgfile = xmalloc(maxstringlen);
- strncpy(cfgfile, cfgpath, maxstringlen);
- stringptr = cfgfile + strlen(cfgpath);
- stringptr[0] = '/';
- stringptr++;
- strncpy(stringptr, optarg, maxstringlen - (cfgfile - stringptr));
- cfgfile[maxstringlen] = '\0';
- free(cfgpath);
- } else
- cfgfile = xstrdup(optarg);
- break;
- case 'v':
- info("monmux version %s", MONMUX_VERSION);
- default:
- info("usage: %s [-d] [-v] [-f cfgfile]", __progname);
- exit(EX_USAGE);
- }
- }
- if (flag_debug != 1) {
- if (daemon(0,0) != 0)
- fatal("daemonize failed");
-
- flag_daemon = 1;
-
- f = fopen(MONMUX_PID_FILE, "w");
- if (f) {
- fprintf(f, "%u\n", (u_int) getpid());
- fclose(f);
- }
- }
-
- info("monmux version %s", MONMUX_VERSION);
-
- if (!read_config_file(&mul, &sol, cfgfile))
- fatal("configuration contained errors; quitting");
- if (flag_debug == 1)
- info("program id=%d", (u_int) getpid());
- mux = SLIST_FIRST(&mul);
- churnbuflen = calculate_churnbuffer(&sol);
- debug("size of churnbuffer = %d", churnbuflen);
- initshare(churnbuflen);
-
- signal(SIGHUP, huphandler);
- signal(SIGINT, exithandler);
- signal(SIGQUIT, exithandler);
- signal(SIGTERM, exithandler);
- signal(SIGTERM, exithandler);
-
- init_crc32();
- getmonsocket(mux);
- getclientsocket(mux);
- for (;;) {
- waitfortraffic(mux, &sol, &source, &packet);
- if (flag_hup == 1) {
- flag_hup = 0;
- SLIST_INIT(&newmul);
- SLIST_INIT(&newsol);
- if (!read_config_file(&newmul, &newsol, cfgfile)) {
- info("new configuration contains errors; keeping old configuration");
- free_muxlist(&newmul);
- free_sourcelist(&newsol);
- } else {
- info("read configuration file succesfully");
- free_muxlist(&mul);
- free_sourcelist(&sol);
- mul = newmul;
- sol = newsol;
- mux = SLIST_FIRST(&mul);
- getmonsocket(mux);
- getclientsocket(mux);
- }
- break;
- }
-
-
- offset = 0;
- maxstringlen = shared_getmaxlen();
-
- master_forbidread();
- timestamp = (time_t) packet.header.timestamp;
- stringbuf = (char *)shared_getmem();
- snprintf(stringbuf, maxstringlen, "%u.%u.%u.%u;",
- IPAS4BYTES(source->ip));
-
-
- maxstringlen -= strlen(stringbuf);
- stringptr = stringbuf + strlen(stringbuf);
-
- while (offset < packet.header.length) {
- offset += sunpack(packet.data + offset, &ps);
-
-
- stream = find_source_stream(source, ps.type, ps.args);
-
- if (stream != NULL) {
-
- snprintf(stringptr, maxstringlen, "%s:", type2str(ps.type));
- maxstringlen -= strlen(stringptr);
- stringptr += strlen(stringptr);
-
- snprintf(stringptr, maxstringlen, "%s:",
- ((ps.args == NULL) ? "0" : ps.args));
- maxstringlen -= strlen(stringptr);
- stringptr += strlen(stringptr);
-
- snprintf(stringptr, maxstringlen, "%u", timestamp);
- arg_ra[3] = stringptr;
- maxstringlen -= strlen(stringptr);
- stringptr += strlen(stringptr);
-
-
- ps2strn(&ps, stringptr, maxstringlen, PS2STR_RRD);
-
- if (stream->file != NULL) {
-
- arg_ra[0] = "rrdupdate";
- arg_ra[1] = "--";
- arg_ra[2] = stream->file;
-
-
- rrd_update(4, arg_ra);
-
- if (rrd_test_error()) {
- warning("rrd_update:%s", rrd_get_error());
- warning("%s %s %s %s", arg_ra[0], arg_ra[1],
- arg_ra[2], arg_ra[3]);
- rrd_clear_error();
- } else {
- if (flag_debug == 1)
- debug("%s %s %s %s", arg_ra[0], arg_ra[1],
- arg_ra[2], arg_ra[3]);
- }
- }
- maxstringlen -= strlen(stringptr);
- stringptr += strlen(stringptr);
- snprintf(stringptr, maxstringlen, ";");
- maxstringlen -= strlen(stringptr);
- stringptr += strlen(stringptr);
- }
- }
-
- snprintf(stringptr, maxstringlen, "\n");
- stringptr += strlen(stringptr);
- shared_setlen((stringptr - stringbuf));
- debug("Churnbuffer used: %d", (stringptr - stringbuf));
- master_permitread();
- }
-
- return (EX_SOFTWARE);
- }
|