public
Description: ScopePort Client: Ruby/Multi-OS
Homepage: http://www.scopeport.org/
Clone URL: git://github.com/lennartkoopmann/scopeport-client-ruby.git
scopeport-client-ruby / scopeport-client.rb
100755 126 lines (109 sloc) 2.926 kb
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/ruby
 
# This file is part of ScopePort (Client Ruby).
#
# Copyright 2009 Lennart Koopmann
#
# ScopePort (Client Ruby) is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ScopePort (Client Ruby) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ScopePort (Client Ruby). If not, see <http://www.gnu.org/licenses/>.
 
hostname = "localhost"
port = 12200
host_id = 1
password = "secret"
 
require "socket"
 
class Conversation
  private
  def socket
    @socket
  end
 
  public
  def initialize hostname, port
    raise "Invalid port" if port < 0 or port > 65535
    @hostname = hostname
    @port = port
  
    # Try to open a socket.
    begin
      @socket = TCPSocket.open(@hostname, @port)
      # Check the answer.
      msg = @socket.recvfrom 100
      raise "Empty or no reply" if msg[0].length == 0
      raise msg[0] if msg[0] != "Okay"
    rescue => e
      raise e
    end
  end
 
  def close
    begin
      @socket.close
    rescue => e
      raise e
    end
  end
 
  def login host_id, password
    begin
      login_message = "#{host_id},login,#{password}"
      @socket.send login_message, login_message.length
      msg = @socket.recvfrom 100
      raise "Empty or no reply" if msg[0].length == 0
      raise msg[0] if msg[0] != "Okay"
    rescue => e
      raise e
    end
  end
 
  def send_sensor_data(host_id, sensor, value)
    raise "Missing host_id" if !host_id.integer?
    raise "Missing sensor name" if sensor.length <= 0
    raise "Missing sensor value" if value.length <= 0
    raise "Illegal characters" if sensor.include? "," or value.include? ","
 
    begin
      sensor_message = "#{host_id},#{sensor},#{value}"
      @socket.send sensor_message, sensor_message.length
      msg = @socket.recvfrom 100
      raise "Empty or no reply" if msg[0].length == 0
      raise msg[0] if msg[0] != "Okay"
    rescue => e
      raise e
    end
  end
end
 
while 1 do
 
  # Connect to the ScopePort server.
  begin
    con = Conversation.new hostname, port
  rescue => e
    puts "Could not connect to ScopePort server: #{e}"
    con.close
    sleep(60)
    next
  end
 
  # Log in
  begin
    con.login host_id, password
  rescue => e
    puts "Could not log in: #{e}"
    con.close
    sleep(60)
    next
  end
 
  # Send sensor data
  begin
    con.send_sensor_data host_id, "sensor_,cpu5", "5.2"
  rescue => e
    puts "Could not send sensor data: #{e}"
    con.close
    sleep(60)
    next
  end
  
  # All done. Close socket.
  con.close
 
  sleep 60
end