/* lnstat - Unified linux network statistics * * Copyright (C) 2004 by Harald Welte * * Development of this code was funded by Astaro AG, http://www.astaro.com/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * */ /* Maximum number of fields that can be displayed */ #define MAX_FIELDS 64 /* Maximum number of header lines */ #define HDR_LINES 10 /* default field width if none specified */ #define FIELD_WIDTH_DEFAULT 8 #define FIELD_WIDTH_MAX 20 #define DEFAULT_INTERVAL 2 #define HDR_LINE_LENGTH (MAX_FIELDS*FIELD_WIDTH_MAX) #include #include #include #include #include #include #include "lnstat.h" struct mysql_handle { char *host; char *database; char *table; char *user; char *password; MYSQL *dbh; char *stmt; char *stmt_val; char *stmt_ins; } static struct mysql_handle mysql_handle; #define MYSQL_INSERTTEMPL "insert into X (Y) values (Z)" #define MYSQL_VALSIZE 100 static int mysql_output(struct mysql_handle *mysql) { int i; for (i = 0; i < fp.num; i++) { sprintf(mysql->stmt_ins, "%lu,", fp.params[i].value); mysql->stmt_ins = mysql->stmt + strlen(mysql->stmt); } *(mysql->stmt_ins -1) = ')'; DEBUGP("stmt=#%s#\n", mysql->stmt); if (mysql_real_query(mysql->dbh, stmt, strlen(stmt))) { fprintf(stderr, "sql error during insert: %s\n", mysql_error(mysql->dbh)); return 1; } return 0; } static int mysql_createstmt(struct mysql_handle *mysql) { struct _field *f; unsigned int size; char buf[255]; int i; if (mysql->stmt) return 1; size = strlen(MYSQL_INSERTTEMPL) + strlen(mysql->table); for (i = 0; i < fp.num; i++) { /* we need space for the key and a comma, as well as * enough space for the values */ size += strlen(fp.params[i].name) + 1 + MYSQL_VALSIZE } mysql->stmt = (char *)malloc(size); if (!mysql->stmt) { fprintf(stderr, "Out of memory\n"); return 1; } sprintf(mysql->stmt, "insert into %s (", mysql->table); mysql->stmt_val = mysql->stmt + strlen(mysql->stmt); for (i = 0; i < fp.num; i++) { /* FIXME: file name */ sprintf(mysql->stmt_val, "%s,", fp.params[i].name); mysql->stmt_val = mysql->stmt + strlen(mysql->stmt); } *(mysql->stmt_val -1) = ')'; sprintf(mysql->stmt_val, " values ("); mysql->stmt_val = mysql->stmt + strlen(mysql->stmt); DEBUGP("stmt='%s'\n", mysql->stmt); return 0; } static int mysql_get_columns(struct mysql_handle *mysql) { MYSQL_RES *result; MYSQL_FIELD *field; struct _field *f; int id; if (mysql->dbh) return 1; result = mysql_list_fields(mysql->dbh, mysql->table, NULL); if (!result) return 1; while ((field = mysql_fetch_field(result))) { char *tok, *key, *file; tok = strchr(field->name, '-'); if (!tok) continue; tok++; /* we never free the string, but this is only called * once */ file = strdup(field->name); key = strdup(tok+1); DEBUGP("field '%s:%s' found\n", file, key); /* add to list of requested keys */ req_files[num_req_files++] = file; fp.params[fp.num++].name = key; } mysql_free_result(result); return 0; } static int mysql_open_db(struct mysql_handle *mysql) { mysql->dbh = mysql_init(NULL); if (!mysql->dbh) return 1; if (!mysql_real_connect(mysql->dbh, mysql->server, mysql->user, mysql->password, mysql->db, 0, NULL, 0)) return 1; return 0; } static int mysql_init(void) { if (mysql_open_db(mysql)) { fprintf(stderr, "Can't open MySQL DB\n"); return -1; } /* read the fieldnames to know which keys we're interested */ if (mysql_get_columns(mysql)) { fprintf(stderr, "unable to get MySQL column names\n"); return -1; } mysql_createstmt(mysql); } static struct option opts[] = { { "version", 0, NULL, 'V' }, { "dump", 1, NULL, 'd' }, { "file", 1, NULL, 'f' }, { "help", 0, NULL, 'h' }, { "host", 1, NULL, 'H' }, { "database", 1, NULL, 'D' }, { "user", 1, NULL, 'U' }, { "pass", 1, NULL, 'P' }, { "interval", 1, NULL, 'i' }, }; static int usage(char *name, int exit_code) { fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION); fprintf(stderr, "Copyright (C) 2004 by Harald Welte " "\n"); fprintf(stderr, "This program is free software licensed under GNU GPLv2" "\nwith ABSOLUTELY NO WARRANTY.\n\n"); fprintf(stderr, "Parameters:\n"); fprintf(stderr, "\t-V --version\t\tPrint Version of Program\n"); fprintf(stderr, "\t-d --dumpt\t\t" "Dump list of available files/keys\n"); fprintf(stderr, "\t-f --file \tStatistics file to use\n"); fprintf(stderr, "\t-h --help\t\tThis help message\n"); fprintf(stderr, "\t-i --interval \t" "Set interval to 'intv' seconds\n"); fprintf(stderr, "\n"); exit(exit_code); } struct field_param { char *name; struct lnstat_field *lf; struct { unsigned int width; } print; }; struct field_params { unsigned int num; struct field_param params[MAX_FIELDS]; }; static void print_line(FILE *of, struct lnstat_file *lnstat_files, struct field_params *fp) { int i; for (i = 0; i < fp->num; i++) { struct lnstat_field *lf = fp->params[i].lf; char formatbuf[255]; snprintf(formatbuf, sizeof(formatbuf)-1, "%%%ulu|", fp->params[i].print.width); fprintf(of, formatbuf, lf->result); } fputc('\n', of); } /* find lnstat_field according to user specification */ static int map_field_params(struct lnstat_file *lnstat_files, struct field_params *fps, int interval) { int i, j = 0; struct lnstat_file *lf; /* no field specification on commandline, need to build default */ if (!fps->num) { for (lf = lnstat_files; lf; lf = lf->next) { for (i = 0; i < lf->num_fields; i++) { fps->params[j].lf = &lf->fields[i]; fps->params[j].lf->file->interval.tv_sec = interval; if (!fps->params[j].print.width) fps->params[j].print.width = FIELD_WIDTH_DEFAULT; j++; } } fps->num = j; return 1; } for (i = 0; i < fps->num; i++) { fps->params[i].lf = lnstat_find_field(lnstat_files, fps->params[i].name); if (!fps->params[i].lf) { fprintf(stderr, "Field `%s' unknown\n", fps->params[i].name); return 0; } fps->params[i].lf->file->interval.tv_sec = interval; if (!fps->params[i].print.width) fps->params[i].print.width = FIELD_WIDTH_DEFAULT; } return 1; } struct table_hdr { int num_lines; char *hdr[HDR_LINES]; }; static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files, struct field_params *fps, int linewidth) { int h,i; static struct table_hdr th; int ofs = 0; for (i = 0; i < HDR_LINES; i++) { th.hdr[i] = malloc(HDR_LINE_LENGTH); memset(th.hdr[i], 0, sizeof(th.hdr[i])); } for (i = 0; i < fps->num; i++) { char *cname, *fname = fps->params[i].lf->name; char fmt[12]; unsigned int width = fps->params[i].print.width; snprintf(fmt, sizeof(fmt)-1, "%%%u.%us|", width, width); snprintf(th.hdr[0]+ofs, width+2, fmt, fps->params[i].lf->file->basename); cname = fname; for (h = 1; h < HDR_LINES; h++) { if (cname - fname >= strlen(fname)) snprintf(th.hdr[h]+ofs, width+2, fmt, ""); else { th.num_lines = h+1; snprintf(th.hdr[h]+ofs, width+2, fmt, cname); } cname += width; } ofs += width+1; } /* fill in spaces */ for (h = 1; h <= th.num_lines; h++) { for (i = 0; i < ofs; i++) { if (th.hdr[h][i] == '\0') th.hdr[h][i] = ' '; } } return &th; } static int print_hdr(FILE *of, struct table_hdr *th) { int i; for (i = 0; i < th->num_lines; i++) { fputs(th->hdr[i], of); fputc('\n', of); } return 0; } int main(int argc, char **argv) { struct lnstat_file *lnstat_files; char *basename; int c; int interval = DEFAULT_INTERVAL; int hdr = 2; enum { MODE_DUMP, MODE_NORMAL, } mode; unsigned long count; struct field_params fp; int num_req_files = 0; char *req_files[LNSTAT_MAX_FILES]; memset(&fp, 0, sizeof(fp)); mode = MODE_NORMAL; /* backwards compatibility mode for old tools */ basename = strrchr(argv[0], '/') + 1; if (!strcmp(basename, "rtstat")) { /* rtstat compatibility mode */ req_files[0] = "rt_cache"; num_req_files = 1; } else if (!strcmp(basename, "ctstat")) { /* ctstat compatibility mode */ req_files[0] = "ip_conntrack"; num_req_files = 1; } while ((c = getopt_long(argc, argv,"Vc:df:h?i:k:s:w:", opts, NULL)) != -1) { switch (c) { int i, len; char *tmp, *tok; case 'c': count = strtoul(optarg, NULL, 0); break; case 'd': mode = MODE_DUMP; break; case 'f': req_files[num_req_files++] = strdup(optarg); break; case '?': case 'h': usage(argv[0], 0); break; case 'i': sscanf(optarg, "%u", &interval); break; case 'k': tmp = strdup(optarg); if (!tmp) break; for (tok = strtok(tmp, ","); tok; tok = strtok(NULL, ",")) { if (fp.num >= MAX_FIELDS) break; fp.params[fp.num++].name = tok; } break; case 's': sscanf(optarg, "%u", &hdr); break; case 'w': tmp = strdup(optarg); if (!tmp) break; i = 0; for (tok = strtok(tmp, ","); tok; tok = strtok(NULL, ",")) { len = strtoul(tok, NULL, 0); if (len > FIELD_WIDTH_MAX) len = FIELD_WIDTH_MAX; fp.params[i].print.width = len; i++; } if (i == 1) { for (i = 0; i < MAX_FIELDS; i++) fp.params[i].print.width = len; } break; default: usage(argv[0], 1); break; } } lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files, (const char **) req_files); switch (mode) { int i; struct table_hdr *header; case MODE_DUMP: lnstat_dump(stderr, lnstat_files); break; case MODE_NORMAL: if (!map_field_params(lnstat_files, &fp, interval)) exit(1); header = build_hdr_string(lnstat_files, &fp, 80); if (!header) exit(1); if (interval < 1 ) interval=1; for (i = 0; i < count; i++) { if ((hdr > 1 && (! (i % 20))) || (hdr == 1 && i == 0)) print_hdr(stdout, header); lnstat_update(lnstat_files); print_line(stdout, lnstat_files, &fp); sleep(interval); } } return 1; }