Skip to content

Commit

Permalink
add util for swap module
Browse files Browse the repository at this point in the history
  • Loading branch information
kongjian committed Sep 25, 2014
1 parent a461e85 commit b828d9d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
67 changes: 55 additions & 12 deletions modules/mod_swap.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "tsar.h"

struct stats_swap {
unsigned long pswpin;
unsigned long pswpout;
unsigned long long pswpin;
unsigned long long pswpout;
unsigned long long swaptotal;
unsigned long long swapfree;
};


Expand All @@ -11,19 +13,20 @@ struct stats_swap {
static char *swap_usage = " --swap swap usage";

/*
*********************************************
* Read swapping statistics from /proc/vmstat.
*********************************************
*************************************************************
* Read swapping statistics from /proc/vmstat & /proc/meminfo.
*************************************************************
*/
static void
read_vmstat_swap(struct module *mod)
{
FILE *fp;
char line[4096], buf[LEN_4096];
char line[LEN_4096], buf[LEN_4096];
struct stats_swap st_swap;

memset(buf, 0, LEN_4096);
memset(&st_swap, 0, sizeof(struct stats_swap));
/* read /proc/vmstat*/
if ((fp = fopen(VMSTAT, "r")) == NULL) {
return ;
}
Expand All @@ -32,27 +35,67 @@ read_vmstat_swap(struct module *mod)

if (!strncmp(line, "pswpin ", 7)) {
/* Read number of swap pages brought in */
sscanf(line + 7, "%lu", &st_swap.pswpin);
sscanf(line + 7, "%llu", &st_swap.pswpin);

} else if (!strncmp(line, "pswpout ", 8)) {
/* Read number of swap pages brought out */
sscanf(line + 8, "%lu", &st_swap.pswpout);
sscanf(line + 8, "%llu", &st_swap.pswpout);
}
}
fclose(fp);
int pos = sprintf(buf, "%ld,%ld", st_swap.pswpin, st_swap.pswpout);
/* read /proc/meminfo */
if ((fp = fopen(MEMINFO, "r")) == NULL) {
return;
}

while (fgets(line, LEN_4096, fp) != NULL) {
if (!strncmp(line, "SwapTotal:", 10)) {
sscanf(line + 10, "%llu", &st_swap.swaptotal);

} else if (!strncmp(line, "SwapFree:", 9)) {
sscanf(line + 9, "%llu", &st_swap.swapfree);
}
}
fclose(fp);

int pos = sprintf(buf, "%lld,%lld,%lld,%lld", st_swap.pswpin, st_swap.pswpout, st_swap.swaptotal*1024, st_swap.swapfree*1024);
buf[pos] = '\0';
set_mod_record(mod, buf);
return;
}

static void
set_swap_record(struct module *mod, double st_array[],
U_64 pre_array[], U_64 cur_array[], int inter)
{
if (cur_array[0] >= pre_array[0]) {
st_array[0] = (cur_array[0] - pre_array[0]) / inter;

} else {
st_array[0] = 0;
}

if (cur_array[1] >= pre_array[1]) {
st_array[1] = (cur_array[1] - pre_array[1]) / inter;

} else {
st_array[1] = 0;
}

/* calc total swap and use util */
st_array[2] = cur_array[2];
st_array[3] = 100.0 - cur_array[3] * 100.0 / cur_array[2];
}

static struct mod_info swap_info[] = {
{" swpin", DETAIL_BIT, 0, STATS_SUB_INTER},
{"swpout", DETAIL_BIT, 0, STATS_SUB_INTER}
{" swpin", DETAIL_BIT, 0, STATS_NULL},
{"swpout", DETAIL_BIT, 0, STATS_NULL},
{" total", DETAIL_BIT, 0, STATS_NULL},
{" util", DETAIL_BIT, 0, STATS_NULL}
};

void
mod_register(struct module *mod)
{
register_mod_fileds(mod, "--swap", swap_usage, swap_info, 2, read_vmstat_swap, NULL);
register_mod_fileds(mod, "--swap", swap_usage, swap_info, 4, read_vmstat_swap, set_swap_record);
}
3 changes: 2 additions & 1 deletion src/framework.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ reload_check_modules()
|| !strcmp(mod->name, "mod_io")
|| !strcmp(mod->name, "mod_tcp")
|| !strcmp(mod->name, "mod_traffic")
|| !strcmp(mod->name, "mod_nginx"))
|| !strcmp(mod->name, "mod_nginx")
|| !strcmp(mod->name, "mod_swap"))
{
mod->enable = 1;

Expand Down
17 changes: 14 additions & 3 deletions src/output_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ running_check(int check_type)
FILE *fp;
char line[2][LEN_40960];
char filename[LEN_128] = {0};
char tmp[9][LEN_4096];
char tmp[10][LEN_4096];
char check[LEN_40960] = {0};
char host_name[LEN_64] = {0};
struct module *mod = NULL;
Expand All @@ -823,7 +823,7 @@ running_check(int check_type)
break;
}
}
memset(tmp, 0, 9 * LEN_4096);
memset(tmp, 0, 10 * LEN_4096);
sprintf(check, "%s\ttsar\t", host_name);
sprintf(filename, "%s", conf.output_file_path);
fp = fopen(filename, "r");
Expand Down Expand Up @@ -1189,8 +1189,19 @@ running_check(int check_type)
}
}
}
if (!strcmp(mod->name, "mod_swap")) {
for (j = 0; j < mod->n_item; j++) {
st_array = &mod->st_array[j * mod->n_col];
if (!st_array || !mod->st_flag) {
sprintf(tmp[9], " swap/total=- swap/util=-");

} else {
sprintf(tmp[9], " swap/total=%0.2f swap/util=%0.2f%%", st_array[2] / 1024 / 1024, st_array[3]);
}
}
}
}
for (j = 0; j < 9; j++) {
for (j = 0; j < 10; j++) {
strcat(check, tmp[j]);
}
printf("%s\n", check);
Expand Down

0 comments on commit b828d9d

Please sign in to comment.