Expose damon_refresh()
This commit is contained in:
parent
36c3aa4efb
commit
2bf3796ddd
395
src/damon.c
395
src/damon.c
|
@ -78,7 +78,7 @@ static void _damon_destroy(DaMon * damon);
|
|||
|
||||
static int _damon_perror(char const * message, int error);
|
||||
|
||||
static int _damon_refresh(DaMon * damon);
|
||||
static int _rrd_update(DaMon * damon, char const * filename, int args_cnt, ...);
|
||||
|
||||
|
||||
/* functions */
|
||||
|
@ -130,6 +130,198 @@ Event * damon_get_event(DaMon * damon)
|
|||
}
|
||||
|
||||
|
||||
/* useful */
|
||||
/* damon_refresh */
|
||||
static AppClient * _refresh_connect(Host * host, Event * event);
|
||||
static int _refresh_uptime(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_load(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_ram(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_swap(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_procs(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_users(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_ifaces(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_vols(AppClient * ac, Host * host, char * rrd);
|
||||
|
||||
int damon_refresh(DaMon * damon)
|
||||
{
|
||||
unsigned int i;
|
||||
AppClient * ac = NULL;
|
||||
char * rrd = NULL;
|
||||
char * p;
|
||||
Host * hosts = damon->hosts;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "DEBUG: %s()\n", __func__);
|
||||
#endif
|
||||
for(i = 0; i < damon->hosts_cnt; i++)
|
||||
{
|
||||
if((ac = hosts[i].appclient) == NULL)
|
||||
if((ac = _refresh_connect(&hosts[i], damon->event))
|
||||
== NULL)
|
||||
continue;
|
||||
if((p = realloc(rrd, string_length(hosts[i].hostname) + 12))
|
||||
== NULL) /* XXX avoid this constant */
|
||||
break;
|
||||
rrd = p;
|
||||
if(_refresh_uptime(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_load(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_ram(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_swap(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_procs(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_users(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_ifaces(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_vols(ac, &hosts[i], rrd) != 0)
|
||||
{
|
||||
appclient_delete(ac);
|
||||
hosts[i].appclient = NULL;
|
||||
continue;
|
||||
}
|
||||
ac = NULL;
|
||||
}
|
||||
free(rrd);
|
||||
if(ac != NULL)
|
||||
fprintf(stderr, "%s", "DaMon: refresh: An error occured\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static AppClient * _refresh_connect(Host * host, Event * event)
|
||||
{
|
||||
if(setenv("APPSERVER_Probe", host->hostname, 1) != 0)
|
||||
return NULL;
|
||||
if((host->appclient = appclient_new_event(NULL, "Probe", NULL, event))
|
||||
== NULL)
|
||||
error_print(PROGNAME);
|
||||
return host->appclient;
|
||||
}
|
||||
|
||||
static int _refresh_uptime(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t ret;
|
||||
|
||||
if(appclient_call(ac, (void **)&ret, "uptime") != 0)
|
||||
return error_print(PROGNAME);
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "uptime.rrd");
|
||||
_rrd_update(host->damon, rrd, 1, ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_load(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
uint32_t load[3];
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "load", &load[0], &load[1],
|
||||
&load[2]) != 0)
|
||||
return error_print(PROGNAME);
|
||||
if(res != 0)
|
||||
return 0;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "load.rrd");
|
||||
_rrd_update(host->damon, rrd, 3, load[0], load[1], load[2]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_procs(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "procs") != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "procs.rrd");
|
||||
_rrd_update(host->damon, rrd, 1, res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_ram(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
uint32_t ram[4];
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "ram", &ram[0], &ram[1], &ram[2],
|
||||
&ram[3]) != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "ram.rrd");
|
||||
_rrd_update(host->damon, rrd, 4, ram[0], ram[1], ram[2], ram[3]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_swap(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
uint32_t swap[2];
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "swap", &swap[0], &swap[1]) != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "swap.rrd");
|
||||
_rrd_update(host->damon, rrd, 2, swap[0], swap[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_users(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "users") != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "users.rrd");
|
||||
_rrd_update(host->damon, rrd, 1, res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _ifaces_if(AppClient * ac, Host * host, char * rrd,
|
||||
char const * iface);
|
||||
static int _refresh_ifaces(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
char ** p = host->ifaces;
|
||||
int ret = 0;
|
||||
|
||||
if(p == NULL)
|
||||
return 0;
|
||||
for(; *p != NULL; p++)
|
||||
ret |= _ifaces_if(ac, host, rrd, *p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int _ifaces_if(AppClient * ac, Host * host, char * rrd,
|
||||
char const * iface)
|
||||
{
|
||||
int32_t res[2];
|
||||
|
||||
if(appclient_call(ac, (void **)&res[0], "ifrxbytes", iface) != 0
|
||||
|| appclient_call(ac, (void **)&res[1], "iftxbytes",
|
||||
iface) != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s%s", host->hostname, DAMON_SEP, iface, ".rrd");
|
||||
_rrd_update(host->damon, rrd, 2, res[0], res[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _vols_vol(AppClient * ac, Host * host, char * rrd, char * vol);
|
||||
static int _refresh_vols(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
char ** p = host->vols;
|
||||
int ret = 0;
|
||||
|
||||
if(p == NULL)
|
||||
return 0;
|
||||
for(; *p != NULL; p++)
|
||||
ret |= _vols_vol(ac, host, rrd, *p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int _vols_vol(AppClient * ac, Host * host, char * rrd, char * vol)
|
||||
{
|
||||
int32_t res[2];
|
||||
|
||||
if(appclient_call(ac, (void **)&res[0], "voltotal", vol) != 0
|
||||
|| appclient_call(ac, (void **)&res[1], "volfree", vol)
|
||||
!= 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%s%s", host->hostname, vol, ".rrd"); /* FIXME */
|
||||
_rrd_update(host->damon, rrd, 2, res[0], res[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* private */
|
||||
/* functions */
|
||||
/* damon_init */
|
||||
|
@ -144,11 +336,11 @@ static int _damon_init(DaMon * damon, char const * config, Event * event)
|
|||
if(_init_config(damon, config) != 0)
|
||||
return 1;
|
||||
damon->event = event;
|
||||
_damon_refresh(damon);
|
||||
damon_refresh(damon);
|
||||
tv.tv_sec = damon->refresh;
|
||||
tv.tv_usec = 0;
|
||||
event_register_timeout(damon->event, &tv,
|
||||
(EventTimeoutFunc)_damon_refresh, damon);
|
||||
(EventTimeoutFunc)damon_refresh, damon);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -322,198 +514,9 @@ static int _damon_perror(char const * message, int ret)
|
|||
}
|
||||
|
||||
|
||||
/* damon_refresh */
|
||||
static AppClient * _refresh_connect(Host * host, Event * event);
|
||||
static int _refresh_uptime(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_load(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_ram(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_swap(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_procs(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_users(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_ifaces(AppClient * ac, Host * host, char * rrd);
|
||||
static int _refresh_vols(AppClient * ac, Host * host, char * rrd);
|
||||
/* rrd_update */
|
||||
static int _update_exec(char * argv[]);
|
||||
|
||||
static int _damon_refresh(DaMon * damon)
|
||||
{
|
||||
unsigned int i;
|
||||
AppClient * ac = NULL;
|
||||
char * rrd = NULL;
|
||||
char * p;
|
||||
Host * hosts = damon->hosts;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "DEBUG: %s()\n", __func__);
|
||||
#endif
|
||||
for(i = 0; i < damon->hosts_cnt; i++)
|
||||
{
|
||||
if((ac = hosts[i].appclient) == NULL)
|
||||
if((ac = _refresh_connect(&hosts[i], damon->event))
|
||||
== NULL)
|
||||
continue;
|
||||
if((p = realloc(rrd, string_length(hosts[i].hostname) + 12))
|
||||
== NULL) /* XXX avoid this constant */
|
||||
break;
|
||||
rrd = p;
|
||||
if(_refresh_uptime(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_load(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_ram(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_swap(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_procs(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_users(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_ifaces(ac, &hosts[i], rrd) != 0
|
||||
|| _refresh_vols(ac, &hosts[i], rrd) != 0)
|
||||
{
|
||||
appclient_delete(ac);
|
||||
hosts[i].appclient = NULL;
|
||||
continue;
|
||||
}
|
||||
ac = NULL;
|
||||
}
|
||||
free(rrd);
|
||||
if(ac != NULL)
|
||||
fprintf(stderr, "%s", "DaMon: refresh: An error occured\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static AppClient * _refresh_connect(Host * host, Event * event)
|
||||
{
|
||||
if(setenv("APPSERVER_Probe", host->hostname, 1) != 0)
|
||||
return NULL;
|
||||
if((host->appclient = appclient_new_event(NULL, "Probe", NULL, event))
|
||||
== NULL)
|
||||
error_print(PROGNAME);
|
||||
return host->appclient;
|
||||
}
|
||||
|
||||
static int _rrd_update(DaMon * damon, char const * filename, int args_cnt, ...);
|
||||
static int _refresh_uptime(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t ret;
|
||||
|
||||
if(appclient_call(ac, (void **)&ret, "uptime") != 0)
|
||||
return error_print(PROGNAME);
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "uptime.rrd");
|
||||
_rrd_update(host->damon, rrd, 1, ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_load(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
uint32_t load[3];
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "load", &load[0], &load[1],
|
||||
&load[2]) != 0)
|
||||
return error_print(PROGNAME);
|
||||
if(res != 0)
|
||||
return 0;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "load.rrd");
|
||||
_rrd_update(host->damon, rrd, 3, load[0], load[1], load[2]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_procs(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "procs") != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "procs.rrd");
|
||||
_rrd_update(host->damon, rrd, 1, res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_ram(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
uint32_t ram[4];
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "ram", &ram[0], &ram[1], &ram[2],
|
||||
&ram[3]) != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "ram.rrd");
|
||||
_rrd_update(host->damon, rrd, 4, ram[0], ram[1], ram[2], ram[3]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_swap(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
uint32_t swap[2];
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "swap", &swap[0], &swap[1]) != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "swap.rrd");
|
||||
_rrd_update(host->damon, rrd, 2, swap[0], swap[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _refresh_users(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
int32_t res;
|
||||
|
||||
if(appclient_call(ac, (void **)&res, "users") != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s", host->hostname, DAMON_SEP, "users.rrd");
|
||||
_rrd_update(host->damon, rrd, 1, res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _ifaces_if(AppClient * ac, Host * host, char * rrd,
|
||||
char const * iface);
|
||||
static int _refresh_ifaces(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
char ** p = host->ifaces;
|
||||
int ret = 0;
|
||||
|
||||
if(p == NULL)
|
||||
return 0;
|
||||
for(; *p != NULL; p++)
|
||||
ret |= _ifaces_if(ac, host, rrd, *p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int _ifaces_if(AppClient * ac, Host * host, char * rrd,
|
||||
char const * iface)
|
||||
{
|
||||
int32_t res[2];
|
||||
|
||||
if(appclient_call(ac, (void **)&res[0], "ifrxbytes", iface) != 0
|
||||
|| appclient_call(ac, (void **)&res[1], "iftxbytes",
|
||||
iface) != 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%c%s%s", host->hostname, DAMON_SEP, iface, ".rrd");
|
||||
_rrd_update(host->damon, rrd, 2, res[0], res[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _vols_vol(AppClient * ac, Host * host, char * rrd, char * vol);
|
||||
static int _refresh_vols(AppClient * ac, Host * host, char * rrd)
|
||||
{
|
||||
char ** p = host->vols;
|
||||
int ret = 0;
|
||||
|
||||
if(p == NULL)
|
||||
return 0;
|
||||
for(; *p != NULL; p++)
|
||||
ret |= _vols_vol(ac, host, rrd, *p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int _vols_vol(AppClient * ac, Host * host, char * rrd, char * vol)
|
||||
{
|
||||
int32_t res[2];
|
||||
|
||||
if(appclient_call(ac, (void **)&res[0], "voltotal", vol) != 0
|
||||
|| appclient_call(ac, (void **)&res[1], "volfree", vol)
|
||||
!= 0)
|
||||
return 1;
|
||||
sprintf(rrd, "%s%s%s", host->hostname, vol, ".rrd"); /* FIXME */
|
||||
_rrd_update(host->damon, rrd, 2, res[0], res[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _exec(char * argv[]);
|
||||
static int _rrd_update(DaMon * damon, char const * filename, int args_cnt, ...)
|
||||
{
|
||||
char * argv[] = { RRDTOOL, "update", NULL, NULL, NULL };
|
||||
|
@ -533,13 +536,13 @@ static int _rrd_update(DaMon * damon, char const * filename, int args_cnt, ...)
|
|||
for(i = 0; i < args_cnt; i++)
|
||||
pos += sprintf(&argv[3][pos], ":%u", va_arg(args, unsigned));
|
||||
va_end(args);
|
||||
ret = _exec(argv);
|
||||
ret = _update_exec(argv);
|
||||
free(argv[3]);
|
||||
string_delete(argv[2]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int _exec(char * argv[])
|
||||
static int _update_exec(char * argv[])
|
||||
{
|
||||
pid_t pid;
|
||||
int status;
|
||||
|
|
|
@ -34,4 +34,7 @@ void damon_delete(DaMon * damon);
|
|||
/* accessors */
|
||||
Event * damon_get_event(DaMon * damon);
|
||||
|
||||
/* useful */
|
||||
int damon_refresh(DaMon * damon);
|
||||
|
||||
#endif /* !DAMON_DAMON_H */
|
||||
|
|
Loading…
Reference in New Issue
Block a user