Class: Xmonitor::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/xmonitor/agent.rb

Defined Under Namespace

Classes: GrabError, PutError

Constant Summary collapse

NEW_LINE =
"\n"
TIME_FORMAT =
'%Y-%m-%d %H:%M:%S'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Agent

Returns a new instance of Agent.



18
19
20
21
22
# File 'lib/xmonitor/agent.rb', line 18

def initialize(config)
  @hostname = Socket.gethostname
  @config = config
  @logger = Logger.new(STDOUT)
end

Class Method Details

.start(argv) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/xmonitor/agent.rb', line 8

def self.start(argv)
  config_file = argv[0]

  config = Xmonitor::Config.from_yaml(config_file)

  agent = self.new(config)

  agent.run
end

Instance Method Details

#create_record(metric, dimension, value) ⇒ Object



126
127
128
# File 'lib/xmonitor/agent.rb', line 126

def create_record(metric, dimension, value)
  {timestamp: Time.now.strftime(TIME_FORMAT), host: @hostname, metric: metric, dimension: dimension, value: value}.to_json
end

#grabObject



67
68
69
70
71
# File 'lib/xmonitor/agent.rb', line 67

def grab
  grab_cpu + grab_memory + grab_disks + grab_network
rescue => e
  raise GrabError.new(e)
end

#grab_cpuObject



73
74
75
76
77
# File 'lib/xmonitor/agent.rb', line 73

def grab_cpu
  PosixPsutil::CPU.cpu_times_percent(1, false).each_pair.map{|(k, v)|
    create_record('cpu', k, v)
  }
end

#grab_disksObject



85
86
87
# File 'lib/xmonitor/agent.rb', line 85

def grab_disks
  grab_disks_usage + grab_disks_io_counter
end

#grab_disks_io_counterObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/xmonitor/agent.rb', line 100

def grab_disks_io_counter
  PosixPsutil::Disks.disk_io_counters.each_pair.map{|(k, v)|
    disk = k

    v.each_pair.map{|k2, v2|
      metric = "#{disk}.#{k2}"
      create_record('disks', metric, v2)
    }
  }.flatten
end

#grab_disks_usageObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/xmonitor/agent.rb', line 89

def grab_disks_usage
  devices = PosixPsutil::Disks.disk_partitions.map{|partition| partition.device}

  devices.map{|device|
    PosixPsutil::Disks.disk_usage(device).each_pair.map{|(k, v)|
      metric = "#{device}.#{k}"
      create_record('disks', metric, v)
    }
  }.flatten
end

#grab_memoryObject



79
80
81
82
83
# File 'lib/xmonitor/agent.rb', line 79

def grab_memory
  PosixPsutil::Memory.virtual_memory.each_pair.map{|(k, v)|
    create_record('memory', k, v)
  }
end

#grab_net_io_counterObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/xmonitor/agent.rb', line 115

def grab_net_io_counter
  PosixPsutil::Network.net_io_counters(true).each_pair.map{|(k, v)|
    interface = k

    v.each_pair.map{|k2, v2|
      metric = "#{interface}.#{k2}"
      create_record('network', metric, v2)
    }
  }.flatten
end

#grab_networkObject



111
112
113
# File 'lib/xmonitor/agent.rb', line 111

def grab_network
  grab_net_io_counter
end

#init_aws_configObject



53
54
55
# File 'lib/xmonitor/agent.rb', line 53

def init_aws_config
  Aws.config[:credentials] = Aws::Credentials.new(@config.access_key_id, @config.secret_access_key)
end

#monitorObject

Raises:



57
58
59
60
61
62
63
64
65
# File 'lib/xmonitor/agent.rb', line 57

def monitor
  records = grab

  raise GrabError if records.empty?

  data = records.join(NEW_LINE) + NEW_LINE

  put_to_firehose(data)
end

#put_to_firehose(data) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/xmonitor/agent.rb', line 130

def put_to_firehose(data)
  client = Aws::Firehose::Client.new(region: @config.region)

  client.put_record({
    delivery_stream_name: @config.stream_name,
    record: {
      data: data,
    },
  })
rescue => e
  raise PutError.new(e)
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/xmonitor/agent.rb', line 24

def run
  init_aws_config

  loop do
    with_log 'monitor' do
      monitor
    end

    with_log 'sleep', true do
      sleep 60
    end
  end
end

#with_log(description, raise_exception = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xmonitor/agent.rb', line 38

def with_log(description, raise_exception = false)
  @logger.info("Begin #{description}")
  begin
    yield
  rescue => e
    if raise_exception
      raise e
    else
      @logger.error(e)
    end
  ensure
    @logger.info("End #{description}")
  end
end