lennartkoopmann-scopeport-client-ruby 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -0
- data/lib/scopeport-client-ruby.rb +150 -0
- data/lib/sensor-linux.rb +2 -0
- metadata +55 -0
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.3
|
@@ -0,0 +1,150 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# This file is part of ScopePort (Client Ruby).
|
4
|
+
#
|
5
|
+
# Copyright 2009 Lennart Koopmann
|
6
|
+
#
|
7
|
+
# ScopePort (Client Ruby) is free software: you can redistribute it and/or
|
8
|
+
# modify it under the terms of the GNU General Public License as published by
|
9
|
+
# the Free Software Foundation, either version 3 of the License, or
|
10
|
+
# (at your option) any later version.
|
11
|
+
#
|
12
|
+
# ScopePort (Client Ruby) is distributed in the hope that it will be useful,
|
13
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
# GNU General Public License for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU General Public License
|
18
|
+
# along with ScopePort (Client Ruby). If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
puts "Starting up (Ruby #{RUBY_VERSION} #{RUBY_PLATFORM})"
|
21
|
+
|
22
|
+
require "sensor-linux.rb"
|
23
|
+
require "socket"
|
24
|
+
|
25
|
+
hostname = "localhost"
|
26
|
+
port = 12200
|
27
|
+
host_id = 1
|
28
|
+
password = "secret"
|
29
|
+
|
30
|
+
class Sensor
|
31
|
+
public
|
32
|
+
def initialize sensor_name
|
33
|
+
@@name = sensor_name
|
34
|
+
@@value = ""
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.get_sensor_names
|
38
|
+
return [ "cpu1", # CPU load average last minute
|
39
|
+
"cpu5", # CPU load average last five minutes
|
40
|
+
"cpu15" # CPU load average last fifteen minutes
|
41
|
+
]
|
42
|
+
end
|
43
|
+
|
44
|
+
def name
|
45
|
+
@@name
|
46
|
+
end
|
47
|
+
|
48
|
+
def value
|
49
|
+
@@value
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
class Conversation
|
55
|
+
public
|
56
|
+
def initialize hostname, port
|
57
|
+
raise "Invalid port" if port < 0 or port > 65535
|
58
|
+
@hostname = hostname
|
59
|
+
@port = port
|
60
|
+
|
61
|
+
# Try to open a socket.
|
62
|
+
begin
|
63
|
+
@@socket = TCPSocket.open(@hostname, @port)
|
64
|
+
# Check the answer.
|
65
|
+
msg = @@socket.recvfrom 100
|
66
|
+
raise "Empty or no reply" if msg[0].length == 0
|
67
|
+
raise msg[0] if msg[0] != "Okay"
|
68
|
+
rescue => e
|
69
|
+
raise e
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def close
|
74
|
+
begin
|
75
|
+
@@socket.close
|
76
|
+
rescue => e
|
77
|
+
raise e
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def login host_id, password
|
82
|
+
begin
|
83
|
+
login_message = "#{host_id},login,#{password}"
|
84
|
+
@@socket.send login_message, login_message.length
|
85
|
+
msg = @@socket.recvfrom 100
|
86
|
+
raise "Empty or no reply" if msg[0].length == 0
|
87
|
+
raise msg[0] if msg[0] != "Okay"
|
88
|
+
rescue => e
|
89
|
+
raise e
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def send_sensor_data(host_id, sensor, value)
|
94
|
+
raise "Missing host_id" if !host_id.integer?
|
95
|
+
raise "Missing sensor name" if sensor.length <= 0
|
96
|
+
raise "Missing sensor value" if value.length <= 0
|
97
|
+
raise "Illegal characters" if sensor.include? "," or value.include? ","
|
98
|
+
|
99
|
+
begin
|
100
|
+
sensor_message = "#{host_id},#{sensor},#{value}"
|
101
|
+
@@socket.send sensor_message, sensor_message.length
|
102
|
+
msg = @@socket.recvfrom 100
|
103
|
+
raise "Empty or no reply" if msg[0].length == 0
|
104
|
+
raise msg[0] if msg[0] != "Okay"
|
105
|
+
rescue => e
|
106
|
+
raise e
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
while 1 do
|
112
|
+
|
113
|
+
# Connect to the ScopePort server.
|
114
|
+
begin
|
115
|
+
con = Conversation.new hostname, port
|
116
|
+
rescue => e
|
117
|
+
puts "Could not connect to ScopePort server: #{e}"
|
118
|
+
sleep(60)
|
119
|
+
next
|
120
|
+
end
|
121
|
+
|
122
|
+
# Log in
|
123
|
+
begin
|
124
|
+
con.login host_id, password
|
125
|
+
rescue => e
|
126
|
+
puts "Could not log in: #{e}"
|
127
|
+
con.close
|
128
|
+
sleep(60)
|
129
|
+
next
|
130
|
+
end
|
131
|
+
|
132
|
+
# Get every sensor and sent it's value.
|
133
|
+
sensor_names = Sensor::get_sensor_names
|
134
|
+
|
135
|
+
sensor_names.each do |sensor_name|
|
136
|
+
sensor = Sensor.new sensor_name
|
137
|
+
puts "Sensor #{sensor.name}: #{sensor.value}"
|
138
|
+
# # Send sensor data
|
139
|
+
# begin
|
140
|
+
# con.send_sensor_data host_id, sensor["name"], sensor["value"]
|
141
|
+
# rescue => e
|
142
|
+
# puts "Could not send sensor data: #{e}"
|
143
|
+
# end
|
144
|
+
end
|
145
|
+
|
146
|
+
# All done. Close socket.
|
147
|
+
con.close
|
148
|
+
|
149
|
+
sleep 60
|
150
|
+
end
|
data/lib/sensor-linux.rb
ADDED
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lennartkoopmann-scopeport-client-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lennart Koopmann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: lennart@scopeport.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- VERSION
|
26
|
+
- lib/scopeport-client-ruby.rb
|
27
|
+
- lib/sensor-linux.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://www.scopeport.org/
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options:
|
32
|
+
- --charset=UTF-8
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.2.0
|
51
|
+
signing_key:
|
52
|
+
specification_version: 2
|
53
|
+
summary: TODO
|
54
|
+
test_files: []
|
55
|
+
|