From db62cf88c6f9a1754dcb60617bb35b538b45d19e Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Tue, 30 Aug 2022 00:03:44 +0100 Subject: [PATCH] Hacked bwbar to output two graphics files with different palettes. --- bwbar.c | 272 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 146 insertions(+), 126 deletions(-) diff --git a/bwbar.c b/bwbar.c index 0a0241e..dba0029 100644 --- a/bwbar.c +++ b/bwbar.c @@ -1,7 +1,8 @@ /* $Id: bwbar.c,v 1.7 2006/08/26 16:31:26 hpa Exp $ */ /* ----------------------------------------------------------------------- * - * + * * Copyright 1999-2006 H. Peter Anvin - All Rights Reserved + * Copyright 2022 Darren 'Tadgy' Austin. * * 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 @@ -13,10 +14,8 @@ /* * bwbar.c - * - * Bandwidth monitor using /proc/net/dev which produces output which can + * Bandwidth monitor using /proc/net/dev, which produces output which can * be displayed on a web page. - * */ #define _GNU_SOURCE @@ -32,6 +31,49 @@ #include #include + +const char *program; + +const struct option longopts[] = { + { "input", 0, 0, 'i' }, + { "output", 0, 0, 'o' }, + { "text-file", 1, 0, 'f' }, + { "light-png", 1, 0, 'l' }, + { "dark-png", 1, 0, 'd' }, + { "interval", 1, 0, 't' }, + { "width", 1, 0, 'x' }, + { "height", 1, 0, 'y' }, + { "border", 1, 0, 'b' }, + { "kbps", 0, 0, 'k' }, + { "Mbps", 0, 0, 'M' }, + { "Gbps", 0, 0, 'G' }, + { "help", 0, 0, 'h' }, + { 0, 0, 0, 0 } +}; + + +void usage(int err) +{ + fprintf(stderr, + "Usage: %s [options] interface max_mbps\n" + "Options: (defaults in parenthesis)\n" + " --input -i Measure input bandwidth\n" + " --output -o Measure output bandwidth (default)\n" + " --text-file -f The name of the text output file\n" + " --light-png -l The name of the light colours graphical bar file\n" + " --dark-png -d The name of the dark colours graphical bar file\n" + " --interval -t The poll interval in seconds (5)\n" + " --width -x Width of the graphical bar (800)\n" + " --height -y Height of the graphical bar (8)\n" + " --border -b Border width of the graphical bar (2)\n" + " --kbps -k Bandwidth is measured in kbit/s\n" + " --Mbps -M Bandwidth is measured in Mbit/s (default)\n" + " --Gbps -G Bandwidth is measured in Gbit/s\n" + " --help -h Display this text\n", + program); + exit(err); +} + void skipline(FILE *f) { int ch; @@ -40,15 +82,8 @@ void skipline(FILE *f) } while ( ch != '\n' && ch != EOF ); } -int write_bar_graph(FILE *f, double perc, int width, int height, int border, int spacers) +int write_bar_graph(FILE *f, static png_color palette, double perc, int width, int height, int border, int spacers) { - static png_color palette[4] = - { - { 0, 0, 0}, /* RGB color for border */ - {255, 255, 255}, /* RGB color for separators */ - {214, 136, 131}, /* RGB color for unfilled part of graph */ - {204, 204, 227}, /* RGB color for filled part of graph */ - }; int bwidth, bheight; /* Width and height including border */ double frac; int spacer, fspacer; @@ -124,7 +159,7 @@ int write_bar_graph(FILE *f, double perc, int width, int height, int border, int *bp++ = color; spacer = fspacer; } - + for ( x = 0 ; x < border ; x++ ) *bp++ = 0; /* Border color */ @@ -145,120 +180,97 @@ int write_bar_graph(FILE *f, double perc, int width, int height, int border, int return status; } -const struct option longopts[] = { - { "input", 0, 0, 'i' }, - { "output", 0, 0, 'o' }, - { "text-file", 1, 0, 'f' }, - { "png-file", 1, 0, 'p' }, - { "interval", 1, 0, 't' }, - { "width", 1, 0, 'x' }, - { "height", 1, 0, 'y' }, - { "border", 1, 0, 'b' }, - { "kbps", 0, 0, 'k' }, - { "Mbps", 0, 0, 'M' }, - { "Gbps", 0, 0, 'G' }, - { "help", 0, 0, 'h' }, - { 0, 0, 0, 0 } -}; - -const char *program; - -void usage(int err) -{ - fprintf(stderr, - "Usage: %s [options] interface max_mbps\n" - "Options: (defaults in parenthesis)\n" - " --input -i Measure input bandwidth\n" - " --output -o Measure output bandwidth (default)\n" - " --text-file -f The name of the text output file (ubar.txt)\n" - " --png-file -p The name of the graphical bar file (ubar.png)\n" - " --interval -t The poll interval in seconds (15)\n" - " --width -x Width of the graphical bar (600)\n" - " --height -y Height of the graphical bar (4)\n" - " --border -b Border width of the graphical bar (1)\n" - " --kbps -k Bandwidth is measured in kbit/s\n" - " --Mbps -M Bandwidth is measured in Mbit/s (default)\n" - " --Gbps -G Bandwidth is measured in Gbit/s\n" - " --help -h Display this text\n", - program); - exit(err); -} - int main(int argc, char *argv[]) { FILE *pnd; - char *interface; + char *interface, *t_tmp, *lg_tmp, *dg_tmp; struct ifinfo { char name[8]; - unsigned int r_bytes, r_pkt, r_err, r_drop, r_fifo, r_frame; - unsigned int r_compr, r_mcast; - unsigned int x_bytes, x_pkt, x_err, x_drop, x_fifo, x_coll; - unsigned int x_carrier, x_compr; + unsigned int r_bytes, r_pkt, r_err, r_drop, r_fifo, r_frame, r_compr, r_mcast; + unsigned int x_bytes, x_pkt, x_err, x_drop, x_fifo, x_coll, x_carrier, x_compr; } ifc; unsigned long long bin, bout, lbin, lbout; - int first; + int first, opt; struct timeval t_now, t_last; - double maxbandwidth; - double bwin, bwout, bwmeasure, timedelta; - int opt; - char *t_tmp, *g_tmp; + double maxbandwidth, bwin, bwout, bwmeasure, timedelta; + /* Options */ - int measure_input = 0; /* Input instead of output */ - char *text_file = "ubar.txt"; /* Text filename */ - char *graphics_file = "ubar.png"; /* Graphics filename */ - char *unit_name = "Mbit/s"; /* Unit name */ - double unit = 1.0e+6; /* Unit multiplier */ - int interval = 15; /* Interval between measurements (s) */ - int width = 600; /* Bar width */ - int height = 4; /* Bar height */ - int border = 1; /* Bar border */ + int measure_input = 0; /* Input instead of output */ + char *text_file = "bwbar.txt"; /* Text filename */ + char *light_graphics_file = "bwbar-light.png"; /* Light bar graphics filename */ + char *dark_graphics_file = "bwbar-dark.png"; /* Dark bar graphics filename */ + char *unit_name = "Mbit/s"; /* Unit name */ + double unit = 1.0e+6; /* Unit multiplier */ + int interval = 5; /* Interval between measurements (s) */ + int width = 800; /* Bar width */ + int height = 8; /* Bar height */ + int border = 2; /* Bar border */ + + /* Palettes */ + static png_color light_palette[4] = + { + { 0, 0, 0}, /* RGB color for border */ + { 0, 0, 0}, /* RGB color for separators */ + { 45, 55, 144}, /* RGB color for filled part of graph */ + {240, 240, 240}, /* RGB color for unfilled part of graph */ + }; + static png_color dark_palette[4] = + { + {255, 255, 255}, /* RGB color for border */ + {255, 255, 255}, /* RGB color for separators */ + { 45, 55, 144}, /* RGB color for filled part of graph */ + { 6, 6, 6}, /* RGB color for unfilled part of graph */ + }; program = argv[0]; while ( (opt = getopt_long(argc, argv, "iof:p:t:x:y:b:kMGh", longopts, NULL)) != -1 ) { switch ( opt ) { - case 'i': - measure_input = 1; - break; - case 'o': - measure_input = 0; - break; - case 'f': - text_file = optarg; - break; - case 'p': - graphics_file = optarg; - break; - case 't': - interval = atoi(optarg); - break; - case 'x': - width = atoi(optarg); - break; - case 'y': - height = atoi(optarg); - break; - case 'b': - border = atoi(optarg); - break; - case 'k': - unit = 1.0e+3; - unit_name = "kbit/s"; - break; - case 'M': - unit = 1.0e+6; - unit_name = "Mbit/s"; - break; - case 'G': - unit = 1.0e+9; - unit_name = "Gbit/s"; - break; - case 'h': - usage(0); - break; - default: - usage(1); - break; + case 'i': + measure_input = 1; + break; + case 'o': + measure_input = 0; + break; + case 'f': + text_file = optarg; + break; + case 'l': + light_graphics_file = optarg; + break; + case 'd': + dark_graphics_file = optarg; + break; + case 't': + interval = atoi(optarg); + break; + case 'x': + width = atoi(optarg); + break; + case 'y': + height = atoi(optarg); + break; + case 'b': + border = atoi(optarg); + break; + case 'k': + unit = 1.0e+3; + unit_name = "kbit/s"; + break; + case 'M': + unit = 1.0e+6; + unit_name = "Mbit/s"; + break; + case 'G': + unit = 1.0e+9; + unit_name = "Gbit/s"; + break; + case 'h': + usage(0); + break; + default: + usage(1); + break; } } @@ -266,13 +278,15 @@ int main(int argc, char *argv[]) usage(1); t_tmp = malloc(strlen(text_file) + 5); - g_tmp = malloc(strlen(graphics_file) + 5); - if ( !t_tmp || !g_tmp ) { + lg_tmp = malloc(strlen(light_graphics_file) + 5); + dg_tmp = malloc(strlen(dark_graphics_file) + 5); + if ( !t_tmp || !lg_tmp || !dg_tmp ) { perror(program); exit(1); } sprintf(t_tmp, "%s.tmp", text_file); - sprintf(g_tmp, "%s.tmp", graphics_file); + sprintf(lg_tmp, "%s.tmp", light_graphics_file); + sprintf(dg_tmp, "%s.tmp", dark_graphics_file); interface = argv[optind]; maxbandwidth = atof(argv[optind+1]) * unit; @@ -281,7 +295,7 @@ int main(int argc, char *argv[]) lbin = 0; lbout = 0; gettimeofday(&t_last, NULL); - + while ( 1 ) { /**** Begin code that obtains bandwidth data ****/ @@ -322,26 +336,32 @@ int main(int argc, char *argv[]) /**** Begin code that generates output ****/ if ( !first ) { - FILE *ubar, *pngmaker; + FILE *ubar, *lpngmaker *dpngmaker; - timedelta = (double)(t_now.tv_sec - t_last.tv_sec) + + timedelta = (double)(t_now.tv_sec - t_last.tv_sec) + (t_now.tv_usec - t_last.tv_usec)/1.0e+6; bwin = (bin-lbin)*8/timedelta; bwout = (bout-lbout)*8/timedelta; bwmeasure = measure_input ? bwin : bwout; - + ubar = fopen(t_tmp, "w"); - pngmaker = fopen(g_tmp, "w"); + lpngmaker = fopen(lg_tmp, "w"); + dpngmaker = fopen(dg_tmp, "w"); if ( ubar ) { - fprintf(ubar, "Current bandwidth utilization %6.2f %s\n", bwmeasure/unit, unit_name); + fprintf(ubar, "%6.2f %s", bwmeasure/unit, unit_name); fclose(ubar); rename(t_tmp, text_file); } - if ( pngmaker ) { - write_bar_graph(pngmaker, bwmeasure/maxbandwidth, width, height, border, 5); - fclose(pngmaker); - rename(g_tmp, graphics_file); + if ( lpngmaker ) { + write_bar_graph(lpngmaker, light_palette, bwmeasure/maxbandwidth, width, height, border, 5); + fclose(lpngmaker); + rename(lg_tmp, light_graphics_file); + } + if ( dpngmaker ) { + write_bar_graph(dpngmaker, dark_palette, bwmeasure/maxbandwidth, width, height, border, 5); + fclose(dpngmaker); + rename(dg_tmp, dark_graphics_file); } } else { first = 0;