Last active
November 27, 2016 17:35
-
-
Save shikachii/d82e6ee914c7ac2211bd0b96723aa5e6 to your computer and use it in GitHub Desktop.
CPUの温度をSlackに投げる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'uri' | |
require File.dirname(__FILE__) + "/slack" # token | |
class Slack | |
def postMessage(message) | |
text = message | |
username = "cputemp" | |
icon_url = "https://pbs.twimg.com/profile_images/802450920269197312/ziE5tTn4.jpg" # -> twitter my icon | |
params = URI.encode_www_form({token: $token, channel: $channel_cpu,text: text,username: username, icon_url: icon_url,as_user: false}) | |
uri = URI.parse("https://slack.com/api/chat.postMessage?#{params}") | |
Net::HTTP.get(uri) | |
end | |
end | |
temp = ["cat /sys/class/thermal/thermal_zone0/temp","cat /sys/class/thermal/thermal_zone1/temp"] | |
# p "℃".unpack("U*") # -> 8451 | |
post = Slack.new | |
message = "" | |
temp.each_with_index do |command,index| | |
m = `#{command}` # -> "27800\n" | |
m.slice!("\n") # -> "27800" | |
m = m.to_i/1000.to_f # -> 27.8 | |
m = "cpu" + index.to_s + " : " + m.to_s + [8451].pack("U*") # -> "cpu0 : 27.8℃" | |
message << m | |
message << ", " if index.zero? | |
end | |
post.postMessage(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment