- style(9)
This commit is contained in:
parent
d94abab5ed
commit
7506f09907
151
ifstatd.c
151
ifstatd.c
@ -1,17 +1,17 @@
|
|||||||
/*-
|
/*-
|
||||||
* Copyright (c) 2015, Babak Farrokhi
|
* Copyright (c) 2015, Babak Farrokhi
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions are met:
|
* modification, are permitted provided that the following conditions are met:
|
||||||
*
|
*
|
||||||
* * Redistributions of source code must retain the above copyright notice, this
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
* list of conditions and the following disclaimer.
|
* list of conditions and the following disclaimer.
|
||||||
*
|
*
|
||||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
* and/or other materials provided with the distribution.
|
* and/or other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
@ -52,28 +52,28 @@
|
|||||||
#define RESOLUTION 10
|
#define RESOLUTION 10
|
||||||
|
|
||||||
struct iftot {
|
struct iftot {
|
||||||
u_long ift_ip; /* input packets */
|
u_long ift_ip; /* input packets */
|
||||||
u_long ift_ie; /* input errors */
|
u_long ift_ie; /* input errors */
|
||||||
u_long ift_id; /* input drops */
|
u_long ift_id; /* input drops */
|
||||||
u_long ift_op; /* output packets */
|
u_long ift_op; /* output packets */
|
||||||
u_long ift_oe; /* output errors */
|
u_long ift_oe; /* output errors */
|
||||||
u_long ift_od; /* output drops */
|
u_long ift_od; /* output drops */
|
||||||
u_long ift_co; /* collisions */
|
u_long ift_co; /* collisions */
|
||||||
u_long ift_ib; /* input bytes */
|
u_long ift_ib; /* input bytes */
|
||||||
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;
|
||||||
struct pidfh *pfh;
|
struct pidfh *pfh;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prepare for a clean shutdown
|
* Prepare for a clean shutdown
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
daemon_shutdown()
|
daemon_shutdown()
|
||||||
{
|
{
|
||||||
pidfile_remove(pfh);
|
pidfile_remove(pfh);
|
||||||
@ -82,7 +82,7 @@ daemon_shutdown()
|
|||||||
/*
|
/*
|
||||||
* Act upon receiving signals
|
* Act upon receiving signals
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
signal_handler(int sig)
|
signal_handler(int sig)
|
||||||
{
|
{
|
||||||
switch (sig) {
|
switch (sig) {
|
||||||
@ -101,11 +101,11 @@ signal_handler(int sig)
|
|||||||
/*
|
/*
|
||||||
* Obtain stats for interface(s).
|
* Obtain stats for interface(s).
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
fill_iftot(struct iftot *st)
|
fill_iftot(struct iftot *st)
|
||||||
{
|
{
|
||||||
struct ifaddrs *ifap, *ifa;
|
struct ifaddrs *ifap, *ifa;
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
|
||||||
if (getifaddrs(&ifap) != 0)
|
if (getifaddrs(&ifap) != 0)
|
||||||
err(EX_OSERR, "getifaddrs");
|
err(EX_OSERR, "getifaddrs");
|
||||||
@ -121,7 +121,6 @@ fill_iftot(struct iftot *st)
|
|||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
st->ift_ip += IFA_STAT(ipackets);
|
st->ift_ip += IFA_STAT(ipackets);
|
||||||
st->ift_ib += IFA_STAT(ibytes);
|
st->ift_ib += IFA_STAT(ibytes);
|
||||||
st->ift_op += IFA_STAT(opackets);
|
st->ift_op += IFA_STAT(opackets);
|
||||||
@ -137,26 +136,29 @@ fill_iftot(struct iftot *st)
|
|||||||
/*
|
/*
|
||||||
* Print out munin plugin configuration
|
* Print out munin plugin configuration
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
config(char *iface)
|
config(char *iface)
|
||||||
{
|
{
|
||||||
printf("graph_order rbytes obytes\n"
|
printf(
|
||||||
"graph_title %s Interface (%d seconds sampling)\n"
|
"graph_order rbytes obytes\n"
|
||||||
"graph_category network\n"
|
"graph_title %s Interface (%d seconds sampling)\n"
|
||||||
"graph_vlabel bits per second\n"
|
"graph_category network\n"
|
||||||
"update_rate %d\n"
|
"graph_vlabel bits per second\n"
|
||||||
"graph_data_size custom 1d, %ds for 1w, 1m for 1t, 5m for 1y\n"
|
"update_rate %d\n"
|
||||||
"rbytes.label received\n"
|
"graph_data_size custom 1d, %ds for 1w, 1m for 1t, 5m for 1y\n"
|
||||||
"rbytes.type DERIVE\n"
|
"rbytes.label received\n"
|
||||||
"rbytes.graph no\n"
|
"rbytes.type DERIVE\n"
|
||||||
"rbytes.cdef rbytes,8,*\n"
|
"rbytes.graph no\n"
|
||||||
"rbytes.min 0\n"
|
"rbytes.cdef rbytes,8,*\n"
|
||||||
"obytes.label bps\n"
|
"rbytes.min 0\n"
|
||||||
"obytes.type DERIVE\n"
|
"obytes.label bps\n"
|
||||||
"obytes.negative rbytes\n"
|
"obytes.type DERIVE\n"
|
||||||
"obytes.cdef obytes,8,*\n"
|
"obytes.negative rbytes\n"
|
||||||
"obytes.min 0\n"
|
"obytes.cdef obytes,8,*\n"
|
||||||
"obytes.draw AREA\n", iface, RESOLUTION, RESOLUTION, RESOLUTION);
|
"obytes.min 0\n"
|
||||||
|
"obytes.draw AREA\n"
|
||||||
|
,iface, RESOLUTION, RESOLUTION, RESOLUTION
|
||||||
|
);
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@ -164,15 +166,16 @@ config(char *iface)
|
|||||||
/*
|
/*
|
||||||
* Wait for a certain amount of time
|
* Wait for a certain amount of time
|
||||||
*/
|
*/
|
||||||
time_t
|
time_t
|
||||||
wait_for(int seconds)
|
wait_for(int seconds)
|
||||||
{
|
{
|
||||||
|
|
||||||
struct timespec tp;
|
struct timespec tp;
|
||||||
|
|
||||||
clock_gettime(CLOCK_REALTIME, &tp);
|
clock_gettime(CLOCK_REALTIME, &tp);
|
||||||
|
|
||||||
time_t current_epoch = tp.tv_sec;
|
time_t current_epoch = tp.tv_sec;
|
||||||
long nsec_to_sleep = 1000 * 1000 * 1000 - tp.tv_nsec;
|
long nsec_to_sleep = 1000 * 1000 * 1000 - tp.tv_nsec;
|
||||||
|
|
||||||
/* Only sleep if needed */
|
/* Only sleep if needed */
|
||||||
if (nsec_to_sleep > 0) {
|
if (nsec_to_sleep > 0) {
|
||||||
@ -180,25 +183,25 @@ wait_for(int seconds)
|
|||||||
tp.tv_nsec = nsec_to_sleep;
|
tp.tv_nsec = nsec_to_sleep;
|
||||||
nanosleep(&tp, NULL);
|
nanosleep(&tp, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
return current_epoch + seconds;
|
return current_epoch + seconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Daemonize and persist pid
|
* Daemonize and persist pid
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
daemon_start()
|
daemon_start()
|
||||||
{
|
{
|
||||||
struct iftot ift, *tot;
|
struct iftot ift, *tot;
|
||||||
time_t epoch;
|
time_t epoch;
|
||||||
struct sigaction sig_action;
|
struct sigaction sig_action;
|
||||||
sigset_t sig_set;
|
sigset_t sig_set;
|
||||||
pid_t otherpid;
|
pid_t otherpid;
|
||||||
|
|
||||||
tot = &ift;
|
tot = &ift;
|
||||||
|
|
||||||
char *no_fork = getenv("no_fork");
|
char *no_fork = getenv("no_fork");
|
||||||
|
|
||||||
if (!no_fork || strcmp("1", no_fork)) {
|
if (!no_fork || strcmp("1", no_fork)) {
|
||||||
|
|
||||||
/* Check if parent process id is set */
|
/* Check if parent process id is set */
|
||||||
@ -206,24 +209,19 @@ daemon_start()
|
|||||||
/* PPID exists, therefore we are already a daemon */
|
/* PPID exists, therefore we are already a daemon */
|
||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if we can acquire the pid file */
|
/* Check if we can acquire the pid file */
|
||||||
pfh = pidfile_open(pid_filename, 0600, &otherpid);
|
pfh = pidfile_open(pid_filename, 0600, &otherpid);
|
||||||
|
|
||||||
if (pfh == NULL) {
|
if (pfh == NULL) {
|
||||||
if (errno == EEXIST) {
|
if (errno == EEXIST) {
|
||||||
errx(EXIT_FAILURE,
|
errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", (intmax_t)otherpid);
|
||||||
"Daemon already running, pid: %jd.",
|
|
||||||
(intmax_t) otherpid);
|
|
||||||
}
|
}
|
||||||
warn("Cannot open or create pidfile: %s", pid_filename);
|
warn("Cannot open or create pidfile: %s", pid_filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fork ourselves if not asked otherwise */
|
/* fork ourselves if not asked otherwise */
|
||||||
if (fork()) {
|
if (fork()) {
|
||||||
return (EXIT_SUCCESS);
|
return (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* we are the child, complete the daemonization */
|
/* we are the child, complete the daemonization */
|
||||||
|
|
||||||
/* Close standard IO */
|
/* Close standard IO */
|
||||||
@ -233,13 +231,14 @@ daemon_start()
|
|||||||
|
|
||||||
/* Block unnecessary signals */
|
/* Block unnecessary signals */
|
||||||
sigemptyset(&sig_set);
|
sigemptyset(&sig_set);
|
||||||
sigaddset(&sig_set, SIGCHLD); /* ignore child - i.e. we don't need
|
sigaddset(&sig_set, SIGCHLD); /* ignore child - i.e. we
|
||||||
* to wait for it */
|
* don't need to wait for it */
|
||||||
sigaddset(&sig_set, SIGTSTP); /* ignore Tty stop signals */
|
sigaddset(&sig_set, SIGTSTP); /* ignore Tty stop signals */
|
||||||
sigaddset(&sig_set, SIGTTOU); /* ignore Tty background writes */
|
sigaddset(&sig_set, SIGTTOU); /* ignore Tty background
|
||||||
|
* writes */
|
||||||
sigaddset(&sig_set, SIGTTIN); /* ignore Tty background reads */
|
sigaddset(&sig_set, SIGTTIN); /* ignore Tty background reads */
|
||||||
sigprocmask(SIG_BLOCK, &sig_set, NULL); /* Block the above specified
|
sigprocmask(SIG_BLOCK, &sig_set, NULL); /* Block the above
|
||||||
* signals */
|
* specified signals */
|
||||||
|
|
||||||
/* Catch necessary signals */
|
/* Catch necessary signals */
|
||||||
sig_action.sa_handler = signal_handler;
|
sig_action.sa_handler = signal_handler;
|
||||||
@ -257,8 +256,7 @@ daemon_start()
|
|||||||
pidfile_write(pfh);
|
pidfile_write(pfh);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
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 */
|
||||||
while (1) {
|
while (1) {
|
||||||
@ -268,8 +266,7 @@ daemon_start()
|
|||||||
flockfile(cache_file);
|
flockfile(cache_file);
|
||||||
|
|
||||||
fill_iftot(tot);
|
fill_iftot(tot);
|
||||||
fprintf(cache_file, "obytes.value %ld:%lu\nrbytes.value %ld:%lu\n",
|
fprintf(cache_file, "obytes.value %ld:%lu\nrbytes.value %ld:%lu\n", epoch, tot->ift_ob, epoch, tot->ift_ib);
|
||||||
epoch, tot->ift_ob, epoch, tot->ift_ib);
|
|
||||||
fflush(cache_file);
|
fflush(cache_file);
|
||||||
|
|
||||||
funlockfile(cache_file);
|
funlockfile(cache_file);
|
||||||
@ -279,17 +276,18 @@ daemon_start()
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
fetch()
|
fetch()
|
||||||
{
|
{
|
||||||
/* this should return data from cache file */
|
/* this should return data from cache file */
|
||||||
FILE *cache_file = fopen(cache_filename, "r+");
|
FILE *cache_file = fopen(cache_filename, "r+");
|
||||||
|
|
||||||
/* lock */
|
/* lock */
|
||||||
flockfile(cache_file);
|
flockfile(cache_file);
|
||||||
|
|
||||||
/* cat the cache_file to stdout */
|
/* cat the cache_file to stdout */
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
|
|
||||||
while (fgets(buffer, 1024, cache_file)) {
|
while (fgets(buffer, 1024, cache_file)) {
|
||||||
printf("%s", buffer);
|
printf("%s", buffer);
|
||||||
}
|
}
|
||||||
@ -300,7 +298,7 @@ fetch()
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argv[0] && argv[0][0])
|
if (argv[0] && argv[0][0])
|
||||||
@ -315,14 +313,14 @@ main(int argc, char *argv[])
|
|||||||
if (strchr(program_name, '_')) {
|
if (strchr(program_name, '_')) {
|
||||||
interface = strchr(program_name, '_') + 1;
|
interface = strchr(program_name, '_') + 1;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
/* program should always run with a valid executable name */
|
* program should always run with a valid executable name
|
||||||
|
*/
|
||||||
if (strlen(interface) < 1) {
|
if (strlen(interface) < 1) {
|
||||||
errx(EX_USAGE, "Please run from symlink");
|
errx(EX_USAGE, "Please run from symlink");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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)
|
if (!MUNIN_PLUGSTATE)
|
||||||
@ -332,15 +330,14 @@ main(int argc, char *argv[])
|
|||||||
asprintf(&cache_filename, "%s/%s.value", MUNIN_PLUGSTATE, program_name);
|
asprintf(&cache_filename, "%s/%s.value", MUNIN_PLUGSTATE, program_name);
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
char *first_arg = argv[1];
|
char *first_arg = argv[1];
|
||||||
|
|
||||||
if (!strcmp(first_arg, "config")) {
|
if (!strcmp(first_arg, "config")) {
|
||||||
return config(interface);
|
return config(interface);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strcmp(first_arg, "acquire")) {
|
if (!strcmp(first_arg, "acquire")) {
|
||||||
return daemon_start();
|
return daemon_start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch();
|
return fetch();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user