Guest User

Untitled

a guest
Feb 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #!/bin/bash
  2. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  3. # sar_graph.sh - Makes pretty web pages from sar
  4. # Written by Damian Zaremba - Released under GPLv3
  5. #
  6. # Please note this is a quick hack to demonstrate it can be done
  7. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  8.  
  9. GNU_PLOT=$(whereis gnuplot | cut -d' ' -f2)
  10. SADF=$(whereis sadf | cut -d' ' -f2)
  11. GRAPH_TYPE="image"
  12. #GRAPH_TYPE="ascii"
  13. HTML_DST="/var/www/vhosts/stuff.damianzaremba.co.uk/public/sar_graph/"
  14. POST_COMMANDS='chown www-server:www-data -R '$HTML_DST
  15.  
  16. # Check gnuplot is all good
  17. if [ ! -x "$GNU_PLOT" ];
  18. then
  19. echo "Please ensure gnuplot is avaible on \$PATH"
  20. exit 1;
  21. fi
  22.  
  23. # Check sadf is all good
  24. if [ ! -x "$SADF" ]
  25. then
  26. echo "Please ensure sadf is avaible on \$PATH"
  27. exit 1;
  28. fi
  29.  
  30. # Check graph type
  31. if [ "$GRAPH_TYPE" != "image" ] && [ "$GRAPH_TYPE" != "ascii" ]
  32. then
  33. echo "Invalid graph type specified"
  34. exit 1;
  35. fi
  36.  
  37. # Some functions for writing out the html, just incase you ever want to make it pretty or something..
  38. function make_header {
  39. HTML="<html><head><title>Sar graphs!</title></head><body>"$HTML;
  40. }
  41.  
  42. function make_footer {
  43. HTML=$HTML"</body></html>";
  44. }
  45.  
  46. function make_graph {
  47. if [ -z "$1" ]; then return 1; else title=$1; fi
  48. if [ -z "$2" ]; then return 1; else format=$2; fi
  49. if [ -z "$3" ]; then return 1; else field=$3; fi
  50.  
  51. echo "Making $title"
  52. output_file=$(echo $title | sed 's/ /_/g')
  53.  
  54. if [ "$GRAPH_TYPE" == "image" ];
  55. then
  56. output_format="png"
  57. output_file=$output_file".png"
  58. else
  59. if [ "$GRAPH_TYPE" == "ascii" ]
  60. then
  61. output_format="dumb"
  62. output_file=$output_file".txt"
  63. fi
  64. fi
  65.  
  66. output_path=$HTML_DST"/"$output_file
  67. dat_file=$(mktemp /tmp/sar_graph.XXX)
  68. echo "Data file: $dat_file"
  69. $SADF -- $format | awk '/'$field'/ {print $3" "$6}' > $dat_file
  70.  
  71. echo "set terminal $output_format;
  72. set title '$title';
  73. set xdata time;
  74. set timefmt '%s';
  75. set xlabel 'Time';
  76. plot '$dat_file' using 1:2 with lines title '$title';" | $GNU_PLOT > $output_path
  77. echo "Outputted $output_file"
  78.  
  79. if [ "$GRAPH_TYPE" == "image" ];
  80. then
  81. HTML=$HTML'<img src="'$output_file'" alt="'$title'" />'
  82. else
  83. if [ "$GRAPH_TYPE" == "ascii" ]
  84. then
  85. HTML=$HTML'<iframe src="'$output_file'" width="650px" height="350px" scrolling="no"></iframe>'
  86. fi
  87. fi
  88. }
  89.  
  90. # Main script stuff
  91. test -d "$HTML_DST" || mkdir -p "$HTML_DST"
  92. HTML=""
  93.  
  94. # CPU stuff
  95. make_graph "CPU usage" "-u" "%idle"
  96.  
  97. # Swap stuff
  98. make_graph "Swap usage" "-r" "kbbuffers"
  99.  
  100. # 1min load avg
  101. make_graph "1min Load avg" "-q" "ldavg-1"
  102.  
  103. # Make the html index
  104. echo "Writing $HTML_DST/index.html"
  105. make_header; make_footer
  106. echo $HTML > "$HTML_DST/index.html"
  107.  
  108. # Post stuff
  109. `$POST_COMMANDS`
  110.  
  111. # Tidy
  112. rm -rf "$base_dir"
Add Comment
Please, Sign In to add comment