Skip to content

Instantly share code, notes, and snippets.

@Naba0123
Created February 5, 2018 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Naba0123/db227a4a6b127dfa7c08f0404073f19c to your computer and use it in GitHub Desktop.
Save Naba0123/db227a4a6b127dfa7c08f0404073f19c to your computer and use it in GitHub Desktop.
1日のCPU平均使用率をツイートする
#!/bin/bash
mpstat 1 1 | sed -n '4,4p' | awk '{ print $13 }' >> ~/ruby/cpu-log
# encoding: utf-8
require 'twitter'
require 'pp'
# conputing cpu average
ave = -1.0
count = -1
max = -1.0
logfile = Dir.home + '/ruby/cpu-log'
if File.exist?(logfile) then
ave = 0.0
count = 0
max = 0.0
File.open(logfile, "r:utf-8") do |f|
while line = f.gets
count += 1
tmp = 100.0 - line.to_f
ave += tmp
if max < tmp then
max = tmp
end
end
end
ave = ave / count
# delete cpu-log
File.unlink logfile
end
# construct tweet
value = `echo "[Server]"`
value << `date +"%Y年%m月%d日%H時%M分" | tr -d '\n'`
value << '取得,過去24時間のCPU使用率の平均は '
value << "%.2f" % ave
value << '% です(最高 '
value << "%.2f" % max
value << `echo "% )"`
# connect Twitter
client = Twitter::REST::Client.new do |config|
config.consumer_key = 'XXX'
config.consumer_secret = 'XXX'
config.access_token = 'XXX'
config.access_token_secret = 'XXX'
end
# Tweet
client.update(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment