simple_host_monitoring 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a3faba04ca0b120d8f7dca660cbef9a0ae97739e
4
+ data.tar.gz: 140fb04b7f7747bd0b8a0c604aab40211d5a47ce
5
+ SHA512:
6
+ metadata.gz: 4b5ea7ec7a40bf6094081aa87b4c6bda5b698d9c389e5fcab9fd2ba7c443cb3678a215007c13d85330ea7b8a67930c7798a9124dae78b8e0193ce6fef2507e61
7
+ data.tar.gz: 207d377f07892903118979bf31828c956e687752e51ad177c24bf703982958e6a11cff4a6d325f3f3411881509de1758dbaa7c174a2389d4e29010c89159edef
@@ -0,0 +1,60 @@
1
+ require_relative '../lib/simple_host_monitoring'
2
+
3
+ #
4
+ parser = BlackStack::SimpleCommandLineParser.new(
5
+ :description => 'This command will launch a SimpleHostMonitoring client that will push the status of the host every X seconds.',
6
+ :configuration => [{
7
+ :name=>'log',
8
+ :mandatory=>false,
9
+ :description=>'Write a logfile with the status of this command.',
10
+ :type=>BlackStack::SimpleCommandLineParser::BOOL,
11
+ :default=>false
12
+ },
13
+ {
14
+ :name=>'log_auto_delete',
15
+ :mandatory=>false,
16
+ :description=>'Write a logfile with the status of this command.',
17
+ :type=>BlackStack::SimpleCommandLineParser::BOOL,
18
+ :default=>true
19
+ },
20
+ {
21
+ :name=>'poll_seconds',
22
+ :mandatory=>false,
23
+ :description=>'How many delay seconds between the starting of a poll cycle and the starting of the next one.',
24
+ :type=>BlackStack::SimpleCommandLineParser::INT,
25
+ :default=>5
26
+ },
27
+ ]
28
+ )
29
+
30
+ #
31
+ logger = BlackStack::LocalLoggerFactory.create('./example01.log')
32
+
33
+ #
34
+ while (true)
35
+ url = "https://127.0.0.1:444/api1.4/shm/update.json"
36
+ api_key = 'E20CBAE0-A4D4-4161-8812-6D9FE67A2E47'
37
+
38
+ logger.logs "Flag start time... "
39
+ start_time = Time.now
40
+ logger.done
41
+
42
+ logger.logs "Poll... "
43
+ host = BlackStack::RemoteHost.new
44
+ host.poll
45
+ logger.done
46
+
47
+ logger.logs "Flag end time... "
48
+ end_time = Time.now
49
+ logger.done
50
+
51
+ logger.logs "Push... "
52
+ host.push(api_key, url)
53
+ logger.done
54
+
55
+ logger.logs "Sleep... "
56
+ enlapsed_seconds = end_time - start_time
57
+ poll_seconds = parser.value('poll_seconds')
58
+ sleep( poll_seconds - enlapsed_seconds ) if poll_seconds > enlapsed_seconds
59
+ logger.done
60
+ end
@@ -0,0 +1,145 @@
1
+ require 'open-uri'
2
+ require 'sys/filesystem'
3
+ require 'sys/cpu'
4
+ require 'pp'
5
+ include Sys
6
+
7
+ module BlackStack
8
+
9
+ module BaseHost
10
+
11
+ # Map the status of this host to the attributes of the class
12
+ # Returns the hash descriptor of the object
13
+ def poll()
14
+ b_total_memory = `wmic ComputerSystem get TotalPhysicalMemory`.delete('^0-9').to_i
15
+ kb_total_memory = b_total_memory / 1024
16
+ mb_total_memory = kb_total_memory / 1024
17
+ gb_total_memory = mb_total_memory / 1024
18
+
19
+ kb_free_memory = `wmic OS get FreePhysicalMemory`.delete('^0-9').to_i
20
+ mb_free_memory = kb_free_memory / 1024
21
+ gb_free_memory = mb_free_memory / 1024
22
+
23
+ # getting disk free space
24
+ stat = Sys::Filesystem.stat("/")
25
+ mb_total_disk = stat.block_size * stat.blocks / 1024 / 1024
26
+ mb_free_disk = stat.block_size * stat.blocks_available / 1024 / 1024
27
+
28
+ # getting public Internet IP
29
+ #remote_ip = remoteIp()
30
+
31
+ # getting server name
32
+ hostname = Socket.gethostname
33
+
34
+ # mapping cpu status
35
+ # self.id = BlackStack::SimpleHostMonitoring::host_id
36
+ self.cpu_architecture = CPU.architecture.to_s
37
+ self.cpu_speed = CPU.freq.to_i
38
+ self.cpu_load_average = CPU.load_avg.to_i
39
+ self.cpu_model = CPU.model.to_s
40
+ self.cpu_type = CPU.cpu_type.to_s
41
+ self.cpu_number = CPU.num_cpu.to_i
42
+
43
+ # mapping ram status
44
+ self.mem_total = mb_total_memory.to_i
45
+ self.mem_free = mb_free_memory.to_i
46
+
47
+ # mapping disk status
48
+ self.disk_total = mb_total_disk.to_i
49
+ self.disk_free = mb_free_disk.to_i
50
+
51
+ # mapping lan attributes
52
+ self.net_hostname = hostname
53
+ #self.net_remote_ip = remote_ip.to_s
54
+ self.net_mac_address = BlackStack::SimpleHostMonitoring.macaddress
55
+
56
+ self.to_hash
57
+ end
58
+
59
+ #
60
+ def to_hash
61
+ {
62
+ # :id => self.id,
63
+ :cpu_architecture => self.cpu_architecture,
64
+ :cpu_speed => self.cpu_speed,
65
+ :cpu_load_average => self.cpu_load_average,
66
+ :cpu_model => self.cpu_model,
67
+ :cpu_type => self.cpu_type,
68
+ :cpu_number => self.cpu_number,
69
+ :mem_total => self.mem_total,
70
+ :mem_free => self.mem_free,
71
+ :disk_total => self.disk_total,
72
+ :disk_free => self.disk_free,
73
+ :net_hostname => self.net_hostname,
74
+ #:net_remote_ip => self.net_remote_ip,
75
+ :net_mac_address => self.net_mac_address
76
+ }
77
+ end
78
+
79
+ #
80
+ def self.valid_descriptor?(h)
81
+ return false if !h.is_a?(Hash)
82
+ # return false if !h[:id].to_s.guid?
83
+ return false if h[:cpu_architecture].to_s.size==0
84
+ return false if !h[:cpu_speed].to_s.fixnum?
85
+ return false if !h[:cpu_load_average].to_s.fixnum?
86
+ return false if h[:cpu_model].to_s.size==0
87
+ return false if h[:cpu_type].to_s.size==0
88
+ return false if !h[:cpu_number].to_s.fixnum?
89
+ return false if !h[:mem_total].to_s.fixnum?
90
+ return false if !h[:mem_free].to_s.fixnum?
91
+ return false if !h[:disk_total].to_s.fixnum?
92
+ return false if !h[:disk_free].to_s.fixnum?
93
+ return false if h[:net_hostname].to_s.size==0
94
+ #return false if h[:net_remote_ip].to_s.size==0
95
+ return false if h[:net_mac_address].to_s.size==0
96
+ true
97
+ end
98
+
99
+ #
100
+ def self.descriptor_validation_details(h)
101
+ s = ''
102
+ s += 'Wrong descriptor format. ' if !h.is_a?(Hash)
103
+ # s += 'Invalid id. ' if !h[:id].to_s.guid?
104
+ s += 'Invalid cpu_architecture. ' if h[:cpu_architecture].to_s.size==0
105
+ s += 'Invalid cpu_speed. ' if !h[:cpu_speed].to_s.fixnum?
106
+ s += 'Invalid cpu_load_average. ' if !h[:cpu_load_average].to_s.fixnum?
107
+ s += 'Invalid cpu_model. ' if h[:cpu_model].to_s.size==0
108
+ s += 'Invalid cpu_type. ' if h[:cpu_type].to_s.size==0
109
+ s += 'Invalid cpu_number. ' if !h[:cpu_number].to_s.fixnum?
110
+ s += 'Invalid mem_total. ' if !h[:mem_total].to_s.fixnum?
111
+ s += 'Invalid mem_free. ' if !h[:mem_free].to_s.fixnum?
112
+ s += 'Invalid disk_total. ' if !h[:disk_total].to_s.fixnum?
113
+ s += 'Invalid disk_free. ' if !h[:disk_free].to_s.fixnum?
114
+ s += 'Invalid net_hostname. ' if h[:net_hostname].to_s.size==0
115
+ #s += 'Invalid :net_remote_ip. ' if h[:net_remote_ip].to_s.size==0
116
+ s += 'Invalid net_mac_address. ' if h[:net_mac_address].to_s.size==0
117
+ s
118
+ end
119
+
120
+ #
121
+ def parse(h)
122
+ # self.id = h[:id]
123
+ self.cpu_architecture = h[:cpu_architecture]
124
+ self.cpu_speed = h[:cpu_speed]
125
+ self.cpu_load_average = h[:cpu_load_average]
126
+ self.cpu_model = h[:cpu_model]
127
+ self.cpu_type = h[:cpu_type]
128
+ self.cpu_number = h[:cpu_number]
129
+ self.mem_total = h[:mem_total]
130
+ self.mem_free = h[:mem_free]
131
+ self.disk_total = h[:disk_total]
132
+ self.disk_free = h[:disk_free]
133
+ self.net_hostname = h[:net_hostname]
134
+ #self.net_remote_ip = h[:net_remote_ip]
135
+ self.net_mac_address = h[:net_mac_address]
136
+ end
137
+
138
+ #
139
+ def push(api_key, url)
140
+ BlackStack::Netting::api_call( url, {:api_key => api_key}.merge(self.to_hash) )
141
+ end
142
+
143
+ end # module BaseHost
144
+
145
+ end # module BlackStack
@@ -0,0 +1,52 @@
1
+ require_relative './basehost'
2
+ require_relative './localhosthistory'
3
+
4
+ module BlackStack
5
+
6
+ class LocalHost < Sequel::Model(:host)
7
+ include BaseHost
8
+ LocalHost.dataset = LocalHost.dataset.disable_insert_output
9
+
10
+ #
11
+ def parse(h)
12
+ self.id = h[:id]
13
+ self.cpu_architecture = h[:cpu_architecture]
14
+ self.cpu_speed = h[:cpu_speed]
15
+ self.cpu_load_average = h[:cpu_load_average]
16
+ self.cpu_model = h[:cpu_model]
17
+ self.cpu_type = h[:cpu_type]
18
+ self.cpu_number = h[:cpu_number]
19
+ self.mem_total = h[:mem_total]
20
+ self.mem_free = h[:mem_free]
21
+ self.disk_total = h[:disk_total]
22
+ self.disk_free = h[:disk_free]
23
+ self.net_hostname = h[:net_hostname]
24
+ #self.net_remote_ip = h[:net_remote_ip]
25
+ self.net_mac_address = h[:net_mac_address]
26
+ end
27
+
28
+ #
29
+ def track
30
+ h = LocalHostHistory.new
31
+ h.id = guid()
32
+ h.id_host = self.id
33
+ h.create_time = now()
34
+ h.cpu_architecture = self.cpu_architecture
35
+ h.cpu_speed = self.cpu_speed
36
+ h.cpu_load_average = self.cpu_load_average
37
+ h.cpu_model = self.cpu_model
38
+ h.cpu_type = self.cpu_type
39
+ h.cpu_number = self.cpu_number
40
+ h.mem_total = self.mem_total
41
+ h.mem_free = self.mem_free
42
+ h.disk_total = self.disk_total
43
+ h.disk_free = self.disk_free
44
+ h.net_hostname = self.net_hostname
45
+ h.net_remote_ip = self.net_remote_ip
46
+ h.net_mac_address = self.net_mac_address
47
+ h.save
48
+ end
49
+
50
+ end # class LocalHost
51
+
52
+ end # module BlackStack
@@ -0,0 +1,10 @@
1
+ require_relative './basehost'
2
+
3
+ module BlackStack
4
+
5
+ class LocalHostHistory < Sequel::Model(:hosthistory)
6
+ LocalHostHistory.dataset = LocalHostHistory.dataset.disable_insert_output
7
+
8
+ end # class LocalHostHistory
9
+
10
+ end # module BlackStack
@@ -0,0 +1,10 @@
1
+ require_relative './basehost'
2
+
3
+ module BlackStack
4
+
5
+ class RemoteHost
6
+ attr_accessor :id, :cpu_architecture, :cpu_speed, :cpu_load_average, :cpu_model, :cpu_type, :cpu_number, :mem_total, :mem_free, :disk_total, :disk_free, :net_hostname, :net_remote_ip, :net_mac_address
7
+ include BaseHost
8
+ end # class RemoteHost
9
+
10
+ end # module BlackStack
@@ -0,0 +1,76 @@
1
+ require 'blackstack_commons'
2
+ require 'simple_command_line_parser'
3
+ require 'simple_cloud_logging'
4
+ #require 'pampa'
5
+
6
+ require 'socket'
7
+ require 'time'
8
+ require 'uri'
9
+ require 'net/http'
10
+ require 'json'
11
+ require 'openssl'
12
+ require 'tiny_tds'
13
+ require 'sequel'
14
+
15
+ require_relative './basehost'
16
+ require_relative './remotehost'
17
+ #require_relative './localhost'
18
+
19
+ module BlackStack
20
+
21
+ module SimpleHostMonitoring
22
+
23
+ # get the unique host id from the file ./host_id.data
24
+ # if the file does not exists, it will ask the API for a GUID and create the file
25
+ HOST_ID_FILENAME = './host_id.data'
26
+
27
+ =begin # derepcated
28
+ @@host_id = nil
29
+ def self.reset_host_id
30
+ @@host_id = BlackStack::Pampa::get_guid
31
+ File.open(HOST_ID_FILENAME, 'w') {|f| f.write(@@host_id) }
32
+ @@host_id
33
+ end
34
+ def self.host_id
35
+ if @@host_id.nil?
36
+ if File.file?(HOST_ID_FILENAME)
37
+ @@host_id = File.read(HOST_ID_FILENAME)
38
+ else
39
+ @@host_id = self.reset_host_id
40
+ end
41
+ end # !@@host_id.nil?
42
+ @@host_id = self.reset_host_id if !@@host_id.guid?
43
+ @@host_id
44
+ end
45
+ =end
46
+ # This function works in windows only
47
+ # TODO: Esta funcion no retorna la mac address completa
48
+ # TODO: Validar que no se retorne una macaddress virtual, con todos valores en 0
49
+ def self.macaddress()
50
+ s = `ipconfig /all`
51
+
52
+ # The standard (IEEE 802) format for printing MAC-48
53
+ # => addresses in human-friendly form is six groups
54
+ # => of two hexadecimal digits, separated by hyphens
55
+ # => - or colons :
56
+ v = s.scan(/(([A-F0-9]{2}\-){5})([A-F0-9]{2}$)/im)
57
+
58
+ if (v.size>0)
59
+ return v.first.join.to_s
60
+ else
61
+ return nil
62
+ end
63
+ end
64
+
65
+ #
66
+ def self.require_db_classes()
67
+ # You have to load all the Sinatra classes after connect the database.
68
+ require_relative '../lib/localhost.rb'
69
+ require_relative '../lib/localhosthistory.rb'
70
+ end
71
+
72
+ end # module SimpleHostMonitoring
73
+
74
+ end # module BlackStack
75
+
76
+
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_host_monitoring
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Leandro Daniel Sardi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: websocket
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.8
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.8
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 1.2.8
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.2.8
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 1.8.1
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.8.1
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 1.8.1
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.8.1
53
+ - !ruby/object:Gem::Dependency
54
+ name: tiny_tds
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 1.0.5
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.0.5
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.5
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.0.5
73
+ - !ruby/object:Gem::Dependency
74
+ name: sequel
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 4.28.0
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 4.28.0
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.28.0
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 4.28.0
93
+ - !ruby/object:Gem::Dependency
94
+ name: blackstack_commons
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: 0.0.10
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 0.0.10
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.0.10
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 0.0.10
113
+ - !ruby/object:Gem::Dependency
114
+ name: simple_cloud_logging
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: 1.1.12
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 1.1.12
123
+ type: :runtime
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: 1.1.12
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 1.1.12
133
+ - !ruby/object:Gem::Dependency
134
+ name: simple_command_line_parser
135
+ requirement: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 1.1.1
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 1.1.1
143
+ type: :runtime
144
+ prerelease: false
145
+ version_requirements: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: 1.1.1
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 1.1.1
153
+ description: 'Find documentation here: https://github.com/leandrosardi/simple_host_monitoring.'
154
+ email: leandro.sardi@expandedventure.com
155
+ executables: []
156
+ extensions: []
157
+ extra_rdoc_files: []
158
+ files:
159
+ - examples/example01.rb
160
+ - lib/basehost.rb
161
+ - lib/localhost.rb
162
+ - lib/localhosthistory.rb
163
+ - lib/remotehost.rb
164
+ - lib/simple_host_monitoring.rb
165
+ homepage: https://rubygems.org/gems/simple_host_monitoring
166
+ licenses:
167
+ - MIT
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 2.4.5.1
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: Track memory, CPU and disk space of any host.
189
+ test_files: []