Skip to content

Instantly share code, notes, and snippets.

@moyashi
Last active March 20, 2016 15:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save moyashi/5345306 to your computer and use it in GitHub Desktop.
Save moyashi/5345306 to your computer and use it in GitHub Desktop.
cronで15分毎にload averageを取得して、それをgnuplotでpngのグラフに書き出すrubyスクリプト gnuplotが必要です。 sudo apt-get install gnuplot cronへの登録例(15分毎の実行) */15 * * * * /var/www/loadaverage.rb
#!/usr/bin/env ruby
# please install gnuplot
# sudo apt-get install gnuplot
# crontab
# */15 * * * * /var/www/loadaverage.rb
logname = "/var/www/loadaverage/#{Time.now.strftime('%Y%m%d')}.log"
pngname = "/var/www/load.png"
File.open("#{logname}", "a") do |f|
f.puts((Time.now+32400).strftime("%s") + " " + `uptime`.split("average: ").last.split(", ")[2])
end
script = <<'EOS'.sub("PNGNAME", "#{pngname}").sub("LOGNAME", "#{logname}")
set terminal png
set output "PNGNAME"
set xdata time
set timefmt "%s"
set grid y
set format x "%H"
set title "Load Average (Daily)"
set size 0.8, 0.5
plot "LOGNAME" using 1:2 title "Load" with lines
EOS
system("echo '#{script}' | gnuplot")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment