File: temperature.rb

package info (click to toggle)
mikutter 3.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,256 kB
  • ctags: 2,165
  • sloc: ruby: 19,079; sh: 205; makefile: 20
file content (90 lines) | stat: -rw-r--r-- 2,832 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#-*- coding: utf-8 -*-
#
# Temperature
#

require File.expand_path(File.join(File.dirname(__FILE__),'..', 'utils'))
miquire :lib, 'sensor'
miquire :lib, 'graph'

if(command_exist?('sensors'))
  Module.new do
    CONFIGFILE = "#{Environment::CONFROOT}temperature"

    def self.boot
      @store = ConfigLoader.create("Plugin::Temperature")
      @sensor = Sensor.new
      getconfig
      graph_reset end

    def self.getconfig
      config = FileTest.exist?(CONFIGFILE) ? confload(CONFIGFILE) : {}
      @identity = config.fetch(:identity, 'C')
      @critical = config.fetch(:critical, 70)
      @max_temp = config.fetch(:max_temp, 100)
      @interval = config.fetch(:interval, 60 * 24) end

    plugin = Plugin::create(:ping)
    plugin.add_event(:period){ |service|
      temps = @sensor.sensor
      graph_temp = temps.dup
      @sensor.hddtemp.each{ |temp|
        graph_temp[temp[:path]] = temp[:temp] }
      self.graph_add(graph_temp, service)
      temp = @sensor.cputemp(temps)
      if temp
        if is_critical?(temp)
          return service.post(:message => "#{temp}#{@identity}なう。熱いです・・・。",
                            :tags => [:tempreture, :critical]) end
        if counter = @store.at(:counter, 0)
          @store.store(:counter, counter.abs - 1) end end }

    def self.is_critical?(temp)
      limit = @store.at(:critical_limit, 0)
      if (temp >= (limit + @critical)) then
        @store.store(:critical_limit, @max_temp - @critical)
        return true
      elsif (limit != 0)
        @store.store(:critical_limit, limit.abs - 1) end
      return false end

    def self.samples(temp)
      samp = @store.at(:samp, [])
      samp.unshift(temp)
      @store.store(:samp, samp[0..@interval])
      return samp[1..@interval+1] end

    def self.graph_add(temps, watch)
      notice temps.inspect
      temps.each{|key, stat|
        if(not @cputemp[key].is_a?(Array)) then
          @cputemp[key] = Array.new
        end
        @cputemp[key][@temp_count] = stat.to_f
      }
      @count_label.call(nil)
      @temp_count += 1
      if(@temp_count >= @interval) then
        watch.post(Graph.drawgraph(@cputemp,
                                   :start => @start_time,
                                   :title => 'Temperature',
                                   :tags => ['temperature'],
                                   :label => @count_label,
                                   :end => Time.now)){ |e, m|
          Delayer.new{ raise m } if e == :fail
        }
        self.graph_reset
      end
      notice "temperature: next graph upload: #{@interval-@temp_count} minutes after"
    end

    def self.graph_reset
      @count_label = Graph.gen_graph_label_defer()
      @temp_count = 0
      @cputemp = Hash.new
      @start_time = Time.now
    end

    boot
  end
end