Commit f93673ed authored by Jon Kåre Hellan's avatar Jon Kåre Hellan
Browse files

Merge forward from mapi_uninett branch:

applications/appmon/appmon.c: Add -d flag to daemonize it.


git-svn-id: file:///home/svn/mapi/trunk@1328 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent c3ddc472
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
......@@ -30,7 +31,10 @@
#define TOP_RESET_TIME 100
extern int daemon_proc;
static void terminate();
static void daemonize(void);
void my_rrd_create(char *filename, int num);
void my_rrd_update(char *, char *);
int create_cgi();
......@@ -81,10 +85,13 @@ int main(int argc, char **argv) {
progname = strdup(argv[0]);
while((opt = getopt(argc, argv, "hvVpas:i:I:n:u:")) != EOF)
while((opt = getopt(argc, argv, "dhvVpas:i:I:n:u:")) != EOF)
{
switch(opt)
{
case 'd':
daemonize();
break;
case 'h':
usage();
break;
......@@ -131,7 +138,8 @@ int main(int argc, char **argv) {
/* look for subnet filters */
if(optind < argc) {
local_net = strdup(argv[optind]);
printf("local_net: %s\n",local_net);
if (!daemon_proc)
printf("local_net: %s\n",local_net);
}
/* start processing */
......@@ -162,12 +170,14 @@ void create_flows(char *scope) {
if (user_net_defined) {
remote_net = user_net;
printf("remote net: %s\n", remote_net);
if (!daemon_proc)
printf("remote net: %s\n", remote_net);
}
else if (local_net) {
remote_net = malloc(MAXLINE);
snprintf(remote_net, MAXLINE, "not %s", local_net);
printf("remote net: %s\n", remote_net);
if (!daemon_proc)
printf("remote net: %s\n", remote_net);
}
......@@ -821,10 +831,8 @@ int create_cgi() {
char cwd[512];
// HOUR
if((fp = fopen(CGI_FILENAME,"w")) == NULL){
printf("\n---> ERROR: Can't create output file\n\n");
return -1;
}
if((fp = fopen(CGI_FILENAME,"w")) == NULL)
err_quit("Can't create output file");
chmod(CGI_FILENAME, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
getcwd(cwd, 512); //FIXME
......@@ -839,10 +847,8 @@ int create_cgi() {
fclose(fp);
// 3 HOURs
if((fp = fopen("appmon3.cgi","w")) == NULL){
printf("\n---> ERROR: Can't create output file\n\n");
return -1;
}
if((fp = fopen("appmon3.cgi","w")) == NULL)
err_quit("Can't create output file");
chmod("appmon3.cgi", S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
getcwd(cwd, 512); //FIXME
......@@ -857,10 +863,8 @@ int create_cgi() {
fclose(fp);
// 1 DAY
if((fp = fopen("appmon24.cgi","w")) == NULL){
printf("\n---> ERROR: Can't create output file\n\n");
return -1;
}
if((fp = fopen("appmon24.cgi","w")) == NULL)
err_quit("Can't create output file");
chmod("appmon24.cgi", S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
getcwd(cwd, 512); //FIXME
......@@ -875,10 +879,8 @@ int create_cgi() {
fclose(fp);
// 1 WEEK
if((fp = fopen("appmonWeek.cgi","w")) == NULL){
printf("\n---> ERROR: Can't create output file\n\n");
return -1;
}
if((fp = fopen("appmonWeek.cgi","w")) == NULL)
err_quit("Can't create output file");
chmod("appmonWeek.cgi", S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
getcwd(cwd, 512); //FIXME
......@@ -893,10 +895,8 @@ int create_cgi() {
fclose(fp);
// 1 MONTH
if((fp = fopen("appmonMonth.cgi","w")) == NULL){
printf("\n---> ERROR: Can't create output file\n\n");
return -1;
}
if((fp = fopen("appmonMonth.cgi","w")) == NULL)
err_quit("Can't create output file");
chmod("appmonMonth.cgi", S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
getcwd(cwd, 512); //FIXME
......@@ -911,10 +911,8 @@ int create_cgi() {
fclose(fp);
// 1 YEAR
if((fp = fopen("appmonYear.cgi","w")) == NULL){
printf("\n---> ERROR: Can't create output file\n\n");
return -1;
}
if((fp = fopen("appmonYear.cgi","w")) == NULL)
err_quit("Can't create output file");
chmod("appmonYear.cgi", S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
getcwd(cwd, 512); //FIXME
......@@ -954,13 +952,15 @@ void my_rrd_create(char *filename, int num) {
for (i=0; i<num; i++) { // loop through all filters (we don't know which are active)
if (filter[i].state == INACTIVE)
continue;
printf("::RRD:: creating records for %d %s\n", i, filter[i].name);
if (!daemon_proc)
printf("::RRD:: creating records for %d %s\n", i, filter[i].name);
snprintf(tmp, 64, "DS:flow%d_in:GAUGE:600:-1000:1000", i);
argv[6+num_active++] = strdup(tmp);
snprintf(tmp, 64, "DS:flow%d_out:GAUGE:600:-1000:1000", i);
argv[6+num_active++] = strdup(tmp);
}
printf("::RRD:: created %d records\n", num_active);
if (!daemon_proc)
printf("::RRD:: created %d records\n", num_active);
argv[6+num_active] = strdup("RRA:AVERAGE:0.5:1:10000"); /* 1 hour */
argv[6+num_active+1] = strdup("RRA:AVERAGE:0.5:6:360"); // 3 hours
argv[6+num_active+2] = strdup("RRA:AVERAGE:0.5:30:360"); // 1 day
......@@ -973,7 +973,10 @@ void my_rrd_create(char *filename, int num) {
ret = rrd_create(13+num_active-2,argv); /* try to create rrd */
//printf("rrd_create: %d\n", ret); /* print result */
if(rrd_test_error()) { /* look for errors */
printf("rrd_create: %s\n",rrd_get_error());
if (daemon_proc)
printf("rrd_create: %s\n",rrd_get_error());
else
syslog(LOG_ERR, "rrd_create: %s\n",rrd_get_error());
rrd_clear_error();
}
//TODO free strdups
......@@ -999,11 +1002,61 @@ void my_rrd_update(char *filename, char *data) {
ret = rrd_update(argc,argv); /* try to update rrd */
//printf("rrd_update: %d\n", ret); /* print result */
if(rrd_test_error()) { /* look for errors */
printf("rrd_update: %s\n",rrd_get_error());
rrd_clear_error();
if (daemon_proc)
printf("rrd_create: %s\n",rrd_get_error());
else
syslog(LOG_ERR, "rrd_update: %s\n",rrd_get_error());
rrd_clear_error();
}
}
static void daemonize() {
int nullfd;
switch (fork()) {
case 0:
break;
case -1:
err_sys ("fork() failed");
return; /* Not reached */
break;
default:
_exit(0);
break;
}
if (setsid() == -1) {
err_sys ("setsid() failed");
return; /* Not reached */
}
setpgrp();
switch (fork()) {
case 0:
break;
case -1:
err_sys ("fork() failed");
return; /* Not reached */
break;
default:
_exit(0);
break;
}
chdir("/");
nullfd = open("/dev/null", O_RDONLY);
dup2(nullfd, STDIN_FILENO);
close(nullfd);
nullfd = open("/dev/null", O_WRONLY);
dup2(nullfd, STDOUT_FILENO);
dup2(nullfd, STDERR_FILENO);
close(nullfd);
daemon_proc = 1;
}
void terminate() {
int i;
for (i=0; i<NUMFILTERS; i++) {
......@@ -1019,6 +1072,7 @@ static char usgtxt[] = "\
%s: appmon: Network Traffic Classification tool using MAPI.\n\
Usage: %s [-hpav] -u <user subnet> [-n <MonitorTitle>] [-s <host:interface>] [localnet]\n\
\t-h\t\tthis page\n\
\t-d\t\trun as daemon\n\
\t-a\t\tanonymize IPs\n\
\t-p\t\tcreate private page with non anonymized IPs\n\
\t-v\t\tverbose\n\
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment