Hacked bwbar to output two graphics files with different palettes.

This commit is contained in:
Darren 'Tadgy' Austin 2022-08-30 00:03:44 +01:00
commit db62cf88c6

262
bwbar.c
View file

@ -2,6 +2,7 @@
/* ----------------------------------------------------------------------- *
*
* 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 <png.h>
#include <zlib.h>
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 <file> -f The name of the text output file\n"
" --light-png <file> -l The name of the light colours graphical bar file\n"
" --dark-png <file> -d The name of the dark colours graphical bar file\n"
" --interval <seconds> -t The poll interval in seconds (5)\n"
" --width <pixels> -x Width of the graphical bar (800)\n"
" --height <pixels> -y Height of the graphical bar (8)\n"
" --border <pixels> -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;
@ -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 <file> -f The name of the text output file (ubar.txt)\n"
" --png-file <file> -p The name of the graphical bar file (ubar.png)\n"
" --interval <seconds> -t The poll interval in seconds (15)\n"
" --width <pixels> -x Width of the graphical bar (600)\n"
" --height <pixels> -y Height of the graphical bar (4)\n"
" --border <pixels> -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;
@ -322,7 +336,7 @@ 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) +
(t_now.tv_usec - t_last.tv_usec)/1.0e+6;
@ -332,16 +346,22 @@ int main(int argc, char *argv[])
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;