Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
- # sar_graph.sh - Makes pretty web pages from sar
- # Written by Damian Zaremba - Released under GPLv3
- #
- # Please note this is a quick hack to demonstrate it can be done
- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
- GNU_PLOT=$(whereis gnuplot | cut -d' ' -f2)
- SADF=$(whereis sadf | cut -d' ' -f2)
- GRAPH_TYPE="image"
- #GRAPH_TYPE="ascii"
- HTML_DST="/var/www/vhosts/stuff.damianzaremba.co.uk/public/sar_graph/"
- POST_COMMANDS='chown www-server:www-data -R '$HTML_DST
- # Check gnuplot is all good
- if [ ! -x "$GNU_PLOT" ];
- then
- echo "Please ensure gnuplot is avaible on \$PATH"
- exit 1;
- fi
- # Check sadf is all good
- if [ ! -x "$SADF" ]
- then
- echo "Please ensure sadf is avaible on \$PATH"
- exit 1;
- fi
- # Check graph type
- if [ "$GRAPH_TYPE" != "image" ] && [ "$GRAPH_TYPE" != "ascii" ]
- then
- echo "Invalid graph type specified"
- exit 1;
- fi
- # Some functions for writing out the html, just incase you ever want to make it pretty or something..
- function make_header {
- HTML="<html><head><title>Sar graphs!</title></head><body>"$HTML;
- }
- function make_footer {
- HTML=$HTML"</body></html>";
- }
- function make_graph {
- if [ -z "$1" ]; then return 1; else title=$1; fi
- if [ -z "$2" ]; then return 1; else format=$2; fi
- if [ -z "$3" ]; then return 1; else field=$3; fi
- echo "Making $title"
- output_file=$(echo $title | sed 's/ /_/g')
- if [ "$GRAPH_TYPE" == "image" ];
- then
- output_format="png"
- output_file=$output_file".png"
- else
- if [ "$GRAPH_TYPE" == "ascii" ]
- then
- output_format="dumb"
- output_file=$output_file".txt"
- fi
- fi
- output_path=$HTML_DST"/"$output_file
- dat_file=$(mktemp /tmp/sar_graph.XXX)
- echo "Data file: $dat_file"
- $SADF -- $format | awk '/'$field'/ {print $3" "$6}' > $dat_file
- echo "set terminal $output_format;
- set title '$title';
- set xdata time;
- set timefmt '%s';
- set xlabel 'Time';
- plot '$dat_file' using 1:2 with lines title '$title';" | $GNU_PLOT > $output_path
- echo "Outputted $output_file"
- if [ "$GRAPH_TYPE" == "image" ];
- then
- HTML=$HTML'<img src="'$output_file'" alt="'$title'" />'
- else
- if [ "$GRAPH_TYPE" == "ascii" ]
- then
- HTML=$HTML'<iframe src="'$output_file'" width="650px" height="350px" scrolling="no"></iframe>'
- fi
- fi
- }
- # Main script stuff
- test -d "$HTML_DST" || mkdir -p "$HTML_DST"
- HTML=""
- # CPU stuff
- make_graph "CPU usage" "-u" "%idle"
- # Swap stuff
- make_graph "Swap usage" "-r" "kbbuffers"
- # 1min load avg
- make_graph "1min Load avg" "-q" "ldavg-1"
- # Make the html index
- echo "Writing $HTML_DST/index.html"
- make_header; make_footer
- echo $HTML > "$HTML_DST/index.html"
- # Post stuff
- `$POST_COMMANDS`
- # Tidy
- rm -rf "$base_dir"
Add Comment
Please, Sign In to add comment