- better error handling
- better pid and cache filename generation
This commit is contained in:
Babak Farrokhi 2015-09-13 16:15:52 +04:30
parent df08fca0d2
commit 936ffce9fe

View File

@ -62,11 +62,11 @@ struct iftot {
u_long ift_ob; /* output bytes */ u_long ift_ob; /* output bytes */
}; };
// Globals /* Globals */
char *program_name = "ifstatd_"; char *program_name = "ifstatd_";
char *interface; char *interface;
char* pid_filename; char *pid_filename;
char* cache_filename; char *cache_filename;
/* /*
* Obtain stats for interface(s). * Obtain stats for interface(s).
@ -93,14 +93,14 @@ fill_iftot(struct iftot *st)
} }
st->ift_ip += IFA_STAT(ipackets); st->ift_ip += IFA_STAT(ipackets);
st->ift_ie += IFA_STAT(ierrors); /*st->ift_ie += IFA_STAT(ierrors);*/
// st->ift_id += IFA_STAT(iqdrops); /*st->ift_id += IFA_STAT(iqdrops);*/
st->ift_ib += IFA_STAT(ibytes); st->ift_ib += IFA_STAT(ibytes);
st->ift_op += IFA_STAT(opackets); st->ift_op += IFA_STAT(opackets);
st->ift_oe += IFA_STAT(oerrors); /*st->ift_oe += IFA_STAT(oerrors);*/
// st->ift_od += IFA_STAT(oqdrops); /* st->ift_od += IFA_STAT(oqdrops);*/
st->ift_ob += IFA_STAT(obytes); st->ift_ob += IFA_STAT(obytes);
st->ift_co += IFA_STAT(collisions); /*st->ift_co += IFA_STAT(collisions);*/
} }
if (interface && found == false) if (interface && found == false)
@ -176,11 +176,11 @@ int acquire()
} }
/* persist pid */ /* persist pid */
FILE* pid_file = fopen(pid_filename, "w"); FILE *pid_file = fopen(pid_filename, "w");
fprintf(pid_file, "%d\n", getpid()); fprintf(pid_file, "%d\n", getpid());
fclose(pid_file); fclose(pid_file);
FILE* cache_file = fopen(cache_filename, "a"); FILE *cache_file = fopen(cache_filename, "a");
/* looping to collect traffic stat every RESOLUTION seconds */ /* looping to collect traffic stat every RESOLUTION seconds */
@ -241,26 +241,17 @@ main(int argc, char* argv[])
/* program should always run with a valid /* program should always run with a valid
executable name */ executable name */
if (strlen(interface) < 1) { if (strlen(interface) < 1) {
printf("please run from symlink\n"); errx(EX_USAGE, "Please run from symlink");
exit(0);
} }
/* resolve paths */ /* resolve paths */
char *MUNIN_PLUGSTATE = getenv("MUNIN_PLUGSTATE"); char *MUNIN_PLUGSTATE = getenv("MUNIN_PLUGSTATE");
/* Default is current directory */ /* Default is current directory */
if (! MUNIN_PLUGSTATE) MUNIN_PLUGSTATE = "."; if (!MUNIN_PLUGSTATE) MUNIN_PLUGSTATE = ".";
size_t MUNIN_PLUGSTATE_length = strlen(MUNIN_PLUGSTATE); asprintf(&pid_filename, "%s/%s.pid", MUNIN_PLUGSTATE, program_name);
asprintf(&cache_filename, "%s/%s.value", MUNIN_PLUGSTATE, program_name);
pid_filename = malloc(MUNIN_PLUGSTATE_length + strlen("/ifstatd.") + strlen("pid") + 1); pid_filename[0] = '\0';
cache_filename = malloc(MUNIN_PLUGSTATE_length + strlen("/ifstatd.") + strlen("value") + 1); cache_filename[0] = '\0';
strcat(pid_filename, MUNIN_PLUGSTATE);
strcat(pid_filename, "/ifstatd.pid");
strcat(cache_filename, MUNIN_PLUGSTATE);
strcat(cache_filename, "/ifstatd.value");
if (argc > 1) { if (argc > 1) {
char* first_arg = argv[1]; char* first_arg = argv[1];