Hacked bwbar to output two graphics files with different palettes.
This commit is contained in:
parent
44d22fe7ec
commit
db62cf88c6
1 changed files with 146 additions and 126 deletions
176
bwbar.c
176
bwbar.c
|
|
@ -2,6 +2,7 @@
|
||||||
/* ----------------------------------------------------------------------- *
|
/* ----------------------------------------------------------------------- *
|
||||||
*
|
*
|
||||||
* Copyright 1999-2006 H. Peter Anvin - All Rights Reserved
|
* 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
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
@ -13,10 +14,8 @@
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* bwbar.c
|
* 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.
|
* be displayed on a web page.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
|
|
@ -32,6 +31,49 @@
|
||||||
#include <png.h>
|
#include <png.h>
|
||||||
#include <zlib.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)
|
void skipline(FILE *f)
|
||||||
{
|
{
|
||||||
int ch;
|
int ch;
|
||||||
|
|
@ -40,15 +82,8 @@ void skipline(FILE *f)
|
||||||
} while ( ch != '\n' && ch != EOF );
|
} 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 */
|
int bwidth, bheight; /* Width and height including border */
|
||||||
double frac;
|
double frac;
|
||||||
int spacer, fspacer;
|
int spacer, fspacer;
|
||||||
|
|
@ -145,73 +180,47 @@ int write_bar_graph(FILE *f, double perc, int width, int height, int border, int
|
||||||
return status;
|
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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
FILE *pnd;
|
FILE *pnd;
|
||||||
char *interface;
|
char *interface, *t_tmp, *lg_tmp, *dg_tmp;
|
||||||
struct ifinfo {
|
struct ifinfo {
|
||||||
char name[8];
|
char name[8];
|
||||||
unsigned int r_bytes, r_pkt, r_err, r_drop, r_fifo, r_frame;
|
unsigned int r_bytes, r_pkt, r_err, r_drop, r_fifo, r_frame, r_compr, r_mcast;
|
||||||
unsigned int r_compr, r_mcast;
|
unsigned int x_bytes, x_pkt, x_err, x_drop, x_fifo, x_coll, x_carrier, x_compr;
|
||||||
unsigned int x_bytes, x_pkt, x_err, x_drop, x_fifo, x_coll;
|
|
||||||
unsigned int x_carrier, x_compr;
|
|
||||||
} ifc;
|
} ifc;
|
||||||
unsigned long long bin, bout, lbin, lbout;
|
unsigned long long bin, bout, lbin, lbout;
|
||||||
int first;
|
int first, opt;
|
||||||
struct timeval t_now, t_last;
|
struct timeval t_now, t_last;
|
||||||
double maxbandwidth;
|
double maxbandwidth, bwin, bwout, bwmeasure, timedelta;
|
||||||
double bwin, bwout, bwmeasure, timedelta;
|
|
||||||
int opt;
|
|
||||||
char *t_tmp, *g_tmp;
|
|
||||||
/* Options */
|
/* Options */
|
||||||
int measure_input = 0; /* Input instead of output */
|
int measure_input = 0; /* Input instead of output */
|
||||||
char *text_file = "ubar.txt"; /* Text filename */
|
char *text_file = "bwbar.txt"; /* Text filename */
|
||||||
char *graphics_file = "ubar.png"; /* Graphics 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 */
|
char *unit_name = "Mbit/s"; /* Unit name */
|
||||||
double unit = 1.0e+6; /* Unit multiplier */
|
double unit = 1.0e+6; /* Unit multiplier */
|
||||||
int interval = 15; /* Interval between measurements (s) */
|
int interval = 5; /* Interval between measurements (s) */
|
||||||
int width = 600; /* Bar width */
|
int width = 800; /* Bar width */
|
||||||
int height = 4; /* Bar height */
|
int height = 8; /* Bar height */
|
||||||
int border = 1; /* Bar border */
|
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];
|
program = argv[0];
|
||||||
|
|
||||||
|
|
@ -226,8 +235,11 @@ int main(int argc, char *argv[])
|
||||||
case 'f':
|
case 'f':
|
||||||
text_file = optarg;
|
text_file = optarg;
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'l':
|
||||||
graphics_file = optarg;
|
light_graphics_file = optarg;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
dark_graphics_file = optarg;
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
interval = atoi(optarg);
|
interval = atoi(optarg);
|
||||||
|
|
@ -266,13 +278,15 @@ int main(int argc, char *argv[])
|
||||||
usage(1);
|
usage(1);
|
||||||
|
|
||||||
t_tmp = malloc(strlen(text_file) + 5);
|
t_tmp = malloc(strlen(text_file) + 5);
|
||||||
g_tmp = malloc(strlen(graphics_file) + 5);
|
lg_tmp = malloc(strlen(light_graphics_file) + 5);
|
||||||
if ( !t_tmp || !g_tmp ) {
|
dg_tmp = malloc(strlen(dark_graphics_file) + 5);
|
||||||
|
if ( !t_tmp || !lg_tmp || !dg_tmp ) {
|
||||||
perror(program);
|
perror(program);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
sprintf(t_tmp, "%s.tmp", text_file);
|
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];
|
interface = argv[optind];
|
||||||
maxbandwidth = atof(argv[optind+1]) * unit;
|
maxbandwidth = atof(argv[optind+1]) * unit;
|
||||||
|
|
@ -322,7 +336,7 @@ int main(int argc, char *argv[])
|
||||||
/**** Begin code that generates output ****/
|
/**** Begin code that generates output ****/
|
||||||
|
|
||||||
if ( !first ) {
|
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;
|
(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;
|
bwmeasure = measure_input ? bwin : bwout;
|
||||||
|
|
||||||
ubar = fopen(t_tmp, "w");
|
ubar = fopen(t_tmp, "w");
|
||||||
pngmaker = fopen(g_tmp, "w");
|
lpngmaker = fopen(lg_tmp, "w");
|
||||||
|
dpngmaker = fopen(dg_tmp, "w");
|
||||||
if ( ubar ) {
|
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);
|
fclose(ubar);
|
||||||
rename(t_tmp, text_file);
|
rename(t_tmp, text_file);
|
||||||
}
|
}
|
||||||
if ( pngmaker ) {
|
if ( lpngmaker ) {
|
||||||
write_bar_graph(pngmaker, bwmeasure/maxbandwidth, width, height, border, 5);
|
write_bar_graph(lpngmaker, light_palette, bwmeasure/maxbandwidth, width, height, border, 5);
|
||||||
fclose(pngmaker);
|
fclose(lpngmaker);
|
||||||
rename(g_tmp, graphics_file);
|
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 {
|
} else {
|
||||||
first = 0;
|
first = 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue