jah 0.0.3 → 0.0.5
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/README.md +62 -13
- data/Rakefile +4 -3
- data/VERSION +1 -1
- data/bin/jah +4 -2
- data/jah.gemspec +73 -21
- data/lib/jah/act_pkg/apt.rb +13 -0
- data/lib/jah/act_pkg/base.rb +17 -0
- data/lib/jah/act_pkg/emerge.rb +4 -0
- data/lib/jah/act_pkg/pacman.rb +59 -0
- data/lib/jah/act_pkg/pkg.rb +52 -0
- data/lib/jah/act_pkg/ports.rb +9 -0
- data/lib/jah/act_pkg/slack.rb +8 -0
- data/lib/jah/act_pkg/urpm.rb +7 -0
- data/lib/jah/act_pkg/yum.rb +31 -0
- data/lib/jah/act_pkg/zypp.rb +12 -0
- data/lib/jah/act_pkg.rb +41 -0
- data/lib/jah/agent.rb +14 -6
- data/lib/jah/agents/xmpp.rb +164 -23
- data/lib/jah/cli.rb +15 -46
- data/lib/jah/command.rb +10 -5
- data/lib/jah/commands/cpu.rb +38 -0
- data/lib/jah/commands/disk.rb +35 -0
- data/lib/jah/commands/mem.rb +49 -0
- data/lib/jah/{collectors/net.rb → commands/netstat.rb} +11 -9
- data/lib/jah/commands/packages.rb +25 -0
- data/lib/jah/commands/prok.rb +97 -0
- data/lib/jah/commands/services.rb +14 -0
- data/lib/jah/commands/status.rb +1 -41
- data/lib/jah/commands/who.rb +27 -0
- data/lib/jah/history.rb +25 -0
- data/lib/jah/install.rb +12 -18
- data/lib/jah/opt.rb +87 -0
- data/lib/jah/prayer.rb +75 -0
- data/lib/jah.rb +21 -13
- data/lib/locales/en_us.yml +9 -1
- data/lib/locales/pt_br.yml +9 -1
- data/spec/jah/act_pkg/apt_spec.rb +40 -0
- data/spec/jah/act_pkg/pacman_spec.rb +141 -0
- data/spec/jah/act_pkg/pkg_spec.rb +51 -0
- data/spec/jah/act_pkg/ports_spec.rb +36 -0
- data/spec/jah/act_pkg/yum_spec.rb +55 -0
- data/spec/jah/act_pkg_spec.rb +41 -0
- data/spec/jah/agent_spec.rb +8 -0
- data/spec/jah/agents/xmpp_spec.rb +8 -0
- data/spec/jah/cli_spec.rb +47 -0
- data/spec/jah/command_spec.rb +5 -0
- data/spec/jah/commands/cpu_spec.rb +51 -0
- data/spec/jah/commands/disk_spec.rb +49 -0
- data/spec/jah/commands/mem_spec.rb +60 -0
- data/spec/jah/commands/netstat_spec.rb +13 -0
- data/spec/jah/commands/prok_spec.rb +248 -0
- data/spec/jah/commands/who_spec.rb +25 -0
- data/spec/jah/history_spec.rb +35 -0
- data/spec/jah/install_spec.rb +15 -0
- data/spec/jah/opt_spec.rb +37 -0
- data/spec/jah/prayer_spec.rb +8 -0
- data/spec/jah_spec.rb +4 -2
- data/spec/spec_helper.rb +3 -1
- metadata +67 -16
- data/lib/jah/collector.rb +0 -12
- data/lib/jah/collectors/cpu.rb +0 -35
- data/lib/jah/collectors/disk.rb +0 -14
- data/lib/jah/collectors/mem.rb +0 -51
- data/lib/jah/collectors/prok.rb +0 -88
- data/lib/jah/collectors/services.rb +0 -13
- data/lib/jah/collectors/who.rb +0 -20
- data/lib/jah/commands/pub.rb +0 -34
- data/lib/jah/god.rb +0 -24
- data/lib/jah.yaml.template +0 -26
data/lib/jah/agents/xmpp.rb
CHANGED
@@ -1,37 +1,50 @@
|
|
1
|
-
require "eventmachine"#autoload :Client,
|
2
1
|
require "blather/client/client"
|
3
2
|
|
4
3
|
module Jah
|
5
4
|
# include Blather::DSL
|
6
5
|
class XmppAgent
|
7
6
|
ROSTER = []
|
7
|
+
PUBSUB = {:pubs => [], :subs => []}
|
8
|
+
PEERS = []
|
9
|
+
|
8
10
|
attr_reader :client
|
9
11
|
|
10
12
|
|
11
13
|
def initialize #(jid, key, server, port=5222, debug=false, report=0)
|
12
|
-
Blather.logger.level = Logger::DEBUG if
|
13
|
-
@report =
|
14
|
-
@client = Blather::Client.setup
|
14
|
+
Blather.logger.level = Logger::DEBUG if Opt.debug
|
15
|
+
@report = Opt.report
|
16
|
+
@client = Blather::Client.setup Opt.jid, Opt.key, Opt.host, Opt.port
|
15
17
|
setup
|
16
18
|
self
|
17
19
|
end
|
18
20
|
|
19
21
|
def process_message(to, msg, type = :chat)
|
22
|
+
args = msg.split(" ")
|
20
23
|
if comm = Jah::Command.find(msg)
|
21
24
|
puts "Commmand => #{comm} | #{msg}"
|
22
|
-
args = msg.split(" ")
|
23
25
|
body = comm[2].send(comm[0], *args[1..-1])
|
26
|
+
elsif pub = PUBSUB[:pubs].find { |x| msg =~ /^#{x}:/ }
|
27
|
+
publish_pub(pub, *args[1..-1])
|
28
|
+
body = "Publishing...."
|
24
29
|
else
|
25
30
|
keywords = %w{ start stop restart monitor unmonitor }
|
26
|
-
kind =
|
27
|
-
|
31
|
+
kind = case msg
|
32
|
+
when /#{keywords.join("|")}/ then :god
|
33
|
+
when /^\!/ then :ruby
|
34
|
+
when /^pub\s/ then :pub
|
35
|
+
else :sh
|
36
|
+
end
|
28
37
|
body = case kind
|
29
|
-
when :ruby
|
30
|
-
puts "Executing ruby command => #{msg}"
|
31
|
-
"=> #{execute_ruby(msg)}"
|
32
38
|
when :sh
|
33
39
|
puts "Executing *sh command => #{msg}"
|
34
40
|
"$> #{execute_sh(msg)}"
|
41
|
+
when :pub
|
42
|
+
PEERS << to.stripped unless PEERS.find { |p| p == [to.stripped] }
|
43
|
+
puts "Executing pubsub command => #{msg}"
|
44
|
+
"P> #{execute_pub(msg)}"
|
45
|
+
when :ruby
|
46
|
+
puts "Executing ruby command => #{msg}"
|
47
|
+
"=> #{execute_ruby(msg)}"
|
35
48
|
when :god
|
36
49
|
puts "Executing god command => #{msg}"
|
37
50
|
"G> #{execute_sh('god ' + msg)}"
|
@@ -71,6 +84,88 @@ module Jah
|
|
71
84
|
res
|
72
85
|
end
|
73
86
|
|
87
|
+
def execute_pub(code)
|
88
|
+
comm = code.split(" ")
|
89
|
+
case comm[1]
|
90
|
+
when "all" then all_pubs
|
91
|
+
when "mine" then my_pubs
|
92
|
+
when "list" then list_pub(comm[2])
|
93
|
+
when "fetch" then fetch_pubs || "Done."
|
94
|
+
when "create" then create_pub(comm[2])
|
95
|
+
when /^sub\w*/ then sub_pub(comm[2])
|
96
|
+
when /^unsub\w*/ then unsub_pub(comm[2])
|
97
|
+
when "destroy" then destroy_pub(comm[2])
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def pub_node
|
103
|
+
@pub_node ||= "pubsub." + client.jid.domain
|
104
|
+
end
|
105
|
+
|
106
|
+
def create_pub(name)
|
107
|
+
client.write Blather::Stanza::PubSub::Create.new(:set,
|
108
|
+
pub_node, name) { |n| yield n if block_given? }
|
109
|
+
PUBSUB[:pubs] << name
|
110
|
+
"Done."
|
111
|
+
end
|
112
|
+
|
113
|
+
def publish_pub(name, *text)
|
114
|
+
client.write Blather::Stanza::PubSub::Publish.new(pub_node, name, :set, text.join)
|
115
|
+
text.join
|
116
|
+
end
|
117
|
+
|
118
|
+
def list_pub(name)
|
119
|
+
client.write Blather::Stanza::PubSub::Items.request(pub_node, name)
|
120
|
+
end
|
121
|
+
|
122
|
+
def destroy_pub(name)
|
123
|
+
client.write Blather::Stanza::PubSubOwner::Delete.new(:set, pub_node, '/' + name)
|
124
|
+
PUBSUB[:pubs] -= [name]
|
125
|
+
"Done."
|
126
|
+
end
|
127
|
+
|
128
|
+
def all_pubs
|
129
|
+
call = Blather::Stanza::DiscoItems.new
|
130
|
+
call.to = pub_node
|
131
|
+
client.write call
|
132
|
+
end
|
133
|
+
|
134
|
+
def my_pubs
|
135
|
+
out = "--- Pubsubs\n"
|
136
|
+
out << "Owner: #{PUBSUB[:pubs].join(', ')}\n" unless PUBSUB[:pubs].empty?
|
137
|
+
out << "Subscribed: #{PUBSUB[:subs].join(', ')}\n" unless PUBSUB[:subs].empty?
|
138
|
+
out == "" ? "No PubSubs." : out
|
139
|
+
end
|
140
|
+
|
141
|
+
def sub_pub(pub)
|
142
|
+
client.write Blather::Stanza::PubSub::Subscribe.new(:set, pub_node, '/' + pub, client.jid.stripped)
|
143
|
+
PUBSUB[:subs] << pub
|
144
|
+
end
|
145
|
+
|
146
|
+
def unsub_pub(pub)
|
147
|
+
client.write Blather::Stanza::PubSub::Unsubscribe.new(:set, pub_node, '/' + pub, client.jid.stripped)
|
148
|
+
PUBSUB[:subs] -= [pub]
|
149
|
+
end
|
150
|
+
|
151
|
+
def fetch_pubs
|
152
|
+
client.write Blather::Stanza::PubSub::Affiliations.new(:get, pub_node)
|
153
|
+
client.write Blather::Stanza::PubSub::Subscriptions.new(:get, pub_node)
|
154
|
+
end
|
155
|
+
|
156
|
+
def alert_peers(msg)
|
157
|
+
PEERS.each do |peer|
|
158
|
+
client.write Blather::Stanza::Message.new(peer, msg)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def process_items(items)
|
163
|
+
items.map do |item|
|
164
|
+
"\nItem: #{item.payload.strip}"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
|
74
169
|
#TODO: need to write raw....
|
75
170
|
def beautify(txt)
|
76
171
|
#txt.gsub!(/\*(.*)\*/, "<span style=\"font-weight: bold;\">\\1</span>")
|
@@ -94,13 +189,15 @@ module Jah
|
|
94
189
|
# return if client && client.setup?
|
95
190
|
client.register_handler(:ready) do
|
96
191
|
puts "Connected!"
|
97
|
-
ROSTER << [client.roster.items.keys,
|
192
|
+
ROSTER << [client.roster.items.keys, Opt.groups].flatten.uniq
|
98
193
|
ROSTER.flatten!
|
99
194
|
ROSTER.select { |j| j =~ /\@conference\./ }.each do |c|
|
100
195
|
presence = Blather::Stanza::Presence.new
|
101
|
-
presence.to = "#{c}/#{
|
196
|
+
presence.to = "#{c}/#{Opt.hostname}"
|
102
197
|
client.write presence
|
103
198
|
end
|
199
|
+
|
200
|
+
fetch_pubs
|
104
201
|
end
|
105
202
|
|
106
203
|
client.register_handler :subscription, :request? do |s|
|
@@ -121,16 +218,61 @@ module Jah
|
|
121
218
|
#client.register_handler :message, :error?, :body do |m|
|
122
219
|
#client.register_handler :message, :headline?, :body do |m|
|
123
220
|
#client.register_handler :message, :normal?, :body do |m|
|
221
|
+
client.register_handler :pubsub_affiliations, :affiliations do |m|
|
222
|
+
puts "[PUB] => #{m.inspect}"
|
223
|
+
m.each do |af|
|
224
|
+
puts "[PUB ITEM] => #{af.inspect}"
|
225
|
+
PUBSUB[:pubs] = af[1].map { |p| p.gsub(/\//, '') }
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
client.register_handler :pubsub_subscriptions, :subscriptions do |m|
|
230
|
+
puts "[SUB] => #{m.inspect}"
|
231
|
+
m.each do |af|
|
232
|
+
puts "[SUB ITEM] => #{af.inspect}"
|
233
|
+
PUBSUB[:subs] = af[1].map { |p| p[:node].gsub(/\//, '') }
|
234
|
+
end
|
235
|
+
end
|
124
236
|
|
125
237
|
client.register_handler :pubsub_event, :items do |m|
|
126
|
-
puts "[PUBSUB] => #{m.inspect}"
|
127
|
-
|
238
|
+
puts "[PUBSUB EV] => #{m.inspect}"
|
239
|
+
alert_peers "PubSub: #{m.node} #{process_items(m.items)}"
|
240
|
+
end
|
241
|
+
|
242
|
+
client.register_handler :pubsub_items, :items do |m|
|
243
|
+
puts "[PUBSUB ITEMS] => #{m.inspect}"
|
244
|
+
alert_peers "PubSub: #{m.node} #{process_items(m.items)}"
|
245
|
+
end
|
246
|
+
|
247
|
+
client.register_handler :disco_items do |r|
|
248
|
+
puts "[ITEM] => #{r}"
|
249
|
+
# Pub.delete_all
|
250
|
+
# PubItem.delete_all
|
251
|
+
for item in r.items
|
252
|
+
puts "[IT] => #{item.name} on #{item.node.class}"
|
253
|
+
# next if item.name =~ /^home$/
|
254
|
+
if item.node =~ /\//
|
255
|
+
puts "[PUBSUB] => #{item.name} on #{item.node}"
|
256
|
+
alert_peers item.name
|
257
|
+
else
|
258
|
+
if item.jid.to_s =~ /conference\./
|
259
|
+
puts "[GROUP] => #{item.name} on #{item.node}"
|
260
|
+
else
|
261
|
+
puts "[USER] => #{item.jid} name #{item.name}"
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
128
265
|
end
|
129
266
|
|
130
267
|
client.register_handler :message, :groupchat? do |m|
|
131
|
-
if m.body =~
|
268
|
+
if m.body =~ Regexp.new(Opt.hostname)
|
269
|
+
body = m.body.split(":")[-1].strip
|
270
|
+
else
|
271
|
+
body = m.body
|
272
|
+
end
|
273
|
+
if m.body =~ /^!|^>|^\\|#{Opt.hostname}/ && m.to_s !~ /x.*:delay/ #delay.nil?
|
132
274
|
puts "[GROUP] => #{m.inspect}"
|
133
|
-
for msg in process_message(m.from.stripped,
|
275
|
+
for msg in process_message(m.from.stripped, body, :groupchat)
|
134
276
|
client.write msg
|
135
277
|
end
|
136
278
|
end
|
@@ -150,18 +292,17 @@ module Jah
|
|
150
292
|
|
151
293
|
def run
|
152
294
|
puts "Starting Jah Client...#{client.jid.stripped}"
|
153
|
-
trap(:INT) { EM.stop }
|
154
|
-
trap(:TERM) { EM.stop }
|
155
295
|
|
156
296
|
EM.run do
|
157
297
|
client.run
|
158
298
|
if @report != 0
|
159
299
|
puts "will report..."
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
300
|
+
# TODO
|
301
|
+
# EM.add_periodic_timer(@report) do
|
302
|
+
# puts " I'm ok.. "
|
303
|
+
# client.write Blather::Stanza::Message.new(
|
304
|
+
# "nofxx@Jah.host.com", Jah::Command.quick_results)
|
305
|
+
# end
|
165
306
|
end
|
166
307
|
|
167
308
|
end
|
data/lib/jah/cli.rb
CHANGED
@@ -3,33 +3,10 @@
|
|
3
3
|
#
|
4
4
|
|
5
5
|
#TODO: def init
|
6
|
-
require
|
6
|
+
require "eventmachine"#autoload :Client,
|
7
7
|
|
8
8
|
module Jah
|
9
9
|
|
10
|
-
HOME = ENV['HOME'] + "/.jah/"
|
11
|
-
unless File.exists? HOME
|
12
|
-
FileUtils.mkdir_p HOME
|
13
|
-
end
|
14
|
-
|
15
|
-
Log = Logger.new(HOME + "jah.log")
|
16
|
-
def Log.write(d); self.warn(d); end
|
17
|
-
$stderr = Log
|
18
|
-
|
19
|
-
Opt = {}
|
20
|
-
|
21
|
-
def self.hostname
|
22
|
-
`hostname`.chomp.gsub(/\W/, "") rescue "jah#{rand(42000)}"
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.locale
|
26
|
-
`locale | grep LANG`.scan(/LANG=(.*)\./)[0][0].downcase rescue "en_us"
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.method_missing(w)
|
30
|
-
Opt[w.to_s.gsub("?", "").to_sym]
|
31
|
-
end
|
32
|
-
|
33
10
|
class Cli
|
34
11
|
|
35
12
|
def self.parse_options(argv)
|
@@ -39,14 +16,14 @@ module Jah
|
|
39
16
|
opts.banner = <<BANNER
|
40
17
|
Jah Gem Usage:
|
41
18
|
|
42
|
-
jah [command] [opts]
|
19
|
+
jah [command] [opts]
|
43
20
|
|
44
21
|
Commands:
|
45
22
|
|
46
23
|
start
|
47
24
|
stop
|
48
25
|
restart
|
49
|
-
|
26
|
+
config
|
50
27
|
|
51
28
|
BANNER
|
52
29
|
opts.separator "Config file:"
|
@@ -92,31 +69,23 @@ BANNER
|
|
92
69
|
end
|
93
70
|
private_class_method :parse_options
|
94
71
|
|
95
|
-
def self.
|
96
|
-
|
72
|
+
def self.work(argv)
|
73
|
+
trap(:INT) { stop! }
|
74
|
+
trap(:TERM) { stop! }
|
75
|
+
|
76
|
+
Opt.autoload_config(parse_options(argv))
|
77
|
+
|
97
78
|
if comm = argv.shift
|
98
|
-
Jah::Agent.send(comm) rescue puts "Command not found: #{comm}"
|
79
|
+
Jah::Agent.send(comm) #rescue puts "Command not found: #{comm} #{@usage}"
|
99
80
|
else
|
100
|
-
|
81
|
+
Opt.mode ? Jah::Agent.start : Install.new
|
101
82
|
end
|
102
83
|
end
|
103
84
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
options = YAML.load(File.read(file + conf)).merge!(options) if file
|
109
|
-
|
110
|
-
# Map acl and group arrays
|
111
|
-
[:acl, :groups].each do |g|
|
112
|
-
options[g] = options[g].split(",").map(&:strip)
|
113
|
-
end
|
114
|
-
options[:groups].map! { |g| "#{g}@conference.#{options[:host]}" }
|
115
|
-
|
116
|
-
# find a better place for this
|
117
|
-
I18n.load_path += Dir[File.join(File.dirname(__FILE__), '..', 'locales', "*.{rb,yml}")]
|
118
|
-
I18n.default_locale = options[:i18n] || "en_us"
|
119
|
-
options
|
85
|
+
def self.stop!
|
86
|
+
puts "Closing Jah..."
|
87
|
+
Opt.closing_time!
|
88
|
+
EM.stop
|
120
89
|
end
|
121
90
|
|
122
91
|
end
|
data/lib/jah/command.rb
CHANGED
@@ -1,21 +1,26 @@
|
|
1
1
|
module Jah
|
2
|
-
REG = []
|
3
2
|
|
4
3
|
module Command
|
4
|
+
COMM = []
|
5
5
|
|
6
6
|
def self.included(base)
|
7
7
|
base.extend ClassMethods
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.find(msg)
|
11
|
-
|
11
|
+
COMM.select { |r| r[1] =~ msg.squeeze(" ").strip }.first
|
12
12
|
end
|
13
13
|
|
14
14
|
module ClassMethods
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
def method_missing(*meth)
|
17
|
+
read
|
18
|
+
@res[meth[0].to_sym]
|
19
|
+
end
|
20
|
+
|
21
|
+
def register(handler, regex)
|
22
|
+
COMM << [handler, /^#{regex}/, self]
|
23
|
+
end
|
19
24
|
|
20
25
|
end
|
21
26
|
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Jah
|
2
|
+
class Cpu
|
3
|
+
include Command
|
4
|
+
register(:load, 'cpu\??$')
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def load
|
9
|
+
read
|
10
|
+
"Med: #{@res[:med]}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def read
|
14
|
+
@res = cpu_load
|
15
|
+
@res[:load] = @res.values.join(", ")
|
16
|
+
@res[:med] = @res[:ten] / @res[:cores] = cores
|
17
|
+
@res
|
18
|
+
end
|
19
|
+
|
20
|
+
def cpu_load
|
21
|
+
if `uptime` =~ /load average(s*): ([\d.]+)(,*) ([\d.]+)(,*) ([\d.]+)\Z/
|
22
|
+
{ :one => $2.to_f,
|
23
|
+
:five => $4.to_f,
|
24
|
+
:ten => $6.to_f }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def cores
|
29
|
+
@cores ||= case RUBY_PLATFORM
|
30
|
+
when /linux/ then `cat /proc/cpuinfo | grep 'model name' | wc -l`.to_i
|
31
|
+
when /darwin/ then `hwprefs cpu_count`.to_i
|
32
|
+
else 1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Jah
|
2
|
+
|
3
|
+
class Disk
|
4
|
+
include Command
|
5
|
+
attr_reader :path, :percent, :total, :free, :used, :mount
|
6
|
+
register(:disk, 'disk\??$')
|
7
|
+
|
8
|
+
def self.read
|
9
|
+
all.map do |d|
|
10
|
+
"\n*#{d[:path]}* => #{d[:percent]}"
|
11
|
+
end.join("\n")
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.all
|
15
|
+
@disks = `df`.to_a.reject { |dl| dl =~ /Size|Use|none/ }.map do |l|
|
16
|
+
new l.split(" ")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(args)
|
21
|
+
@path, @total, @used, @free, @percent, @mount = args
|
22
|
+
@total, @used, @free = [@total, @used, @free].map(&:to_i)
|
23
|
+
@percent = @percent[0..-2].to_i
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.mount
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.umount
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Jah
|
2
|
+
|
3
|
+
class Mem
|
4
|
+
include Command
|
5
|
+
register(:snap, 'mem\??$')
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def snap
|
10
|
+
read
|
11
|
+
"Free: #{@res[:free]} Used: #{@res[:used]} #{@res[:percent]}%"
|
12
|
+
end
|
13
|
+
|
14
|
+
def read
|
15
|
+
mem_info = {}
|
16
|
+
`cat /proc/meminfo`.each do |line|
|
17
|
+
_, key, value = *line.match(/^(\w+):\s+(\d+)\s/)
|
18
|
+
mem_info[key] = value.to_i
|
19
|
+
end
|
20
|
+
|
21
|
+
# memory info is empty - operating system may not support it (why doesn't an exception get raised earlier on mac osx?)
|
22
|
+
raise "No such file or directory" if mem_info.empty?
|
23
|
+
@res = {}
|
24
|
+
@res[:total] = total = mem_info['MemTotal'] / 1024
|
25
|
+
@res[:free] = free = (mem_info['MemFree'] + mem_info['Buffers'] + mem_info['Cached']) / 1024
|
26
|
+
@res[:used] = total - free
|
27
|
+
@res[:percent] = (free / total.to_f * 100).to_i
|
28
|
+
|
29
|
+
@res[:swap_total] = stotal = mem_info['SwapTotal'] / 1024
|
30
|
+
@res[:swap_free] = sfree = mem_info['SwapFree'] / 1024
|
31
|
+
# @res[:swap_used] = stotal - sfree
|
32
|
+
# unless stotal == 0
|
33
|
+
# @swap_percent = (@swap_used / @swap_total.to_f * 100).to_i
|
34
|
+
# end
|
35
|
+
rescue Exception => e
|
36
|
+
if e.message =~ /No such file or directory/
|
37
|
+
puts "/proc/meminfo not found.. trying top!"
|
38
|
+
top = `top -l 1`.to_a[5].split.map!{|m| m[0..-2].to_i}.reject(&:zero?)
|
39
|
+
@res[:used], @res[:free] = top[3,4]
|
40
|
+
@res[:total] = @res[:used] + @res[:free]
|
41
|
+
@res[:percent] = (@res[:free] / @res[:total].to_f * 100).to_i
|
42
|
+
else
|
43
|
+
raise e
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -1,9 +1,17 @@
|
|
1
1
|
module Jah
|
2
|
-
class
|
3
|
-
|
2
|
+
class Netstat
|
3
|
+
include Command
|
4
|
+
register(:net, 'net\??$')
|
5
|
+
|
4
6
|
class << self
|
5
|
-
# attr_reader :count, :ips
|
6
7
|
|
8
|
+
def net
|
9
|
+
out = ""
|
10
|
+
connections.each do |c|
|
11
|
+
out << "#{c[0]} => #{c[1]} connections\n"
|
12
|
+
end
|
13
|
+
out << "Total: #{Net.count}"
|
14
|
+
end
|
7
15
|
|
8
16
|
def count
|
9
17
|
`netstat -n | grep -i established | wc -l`.to_i
|
@@ -17,11 +25,5 @@ module Jah
|
|
17
25
|
end
|
18
26
|
end
|
19
27
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
28
|
end
|
27
29
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Jah
|
2
|
+
|
3
|
+
class Packages
|
4
|
+
include Command
|
5
|
+
register(:install, "install\s")
|
6
|
+
register(:pkg, "pkg\s")
|
7
|
+
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def install(foo)
|
12
|
+
ActPkg.install(foo)
|
13
|
+
end
|
14
|
+
|
15
|
+
def pkg(*meths)
|
16
|
+
case res = ActPkg.send(*meths)
|
17
|
+
when Array
|
18
|
+
res.map { |i| ">#{i.name} #{i.version}\n" }
|
19
|
+
when String then res
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module Jah
|
2
|
+
|
3
|
+
class Prok
|
4
|
+
include Command
|
5
|
+
attr_reader :user, :pid, :comm, :cpu, :mem, :rss, :vsz, :stat, :tty, :time
|
6
|
+
BANLIST = [/^ata/, /^init$/, /^scsi_/, /\/\d$/, /agetty/ ]
|
7
|
+
register(:read, 'proks?(\s|$)|top$')
|
8
|
+
|
9
|
+
def self.read(find = nil)
|
10
|
+
find ? Prok.find(find).to_s : Prok.all.map(&:to_s)
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.all
|
15
|
+
`ps auxww`.to_a[1..-1].map do |l|
|
16
|
+
new(l.split)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.find(comm_or_pid)
|
21
|
+
all.select do |p|
|
22
|
+
unless comm_or_pid.to_i.zero?
|
23
|
+
p.pid == comm_or_pid.to_i
|
24
|
+
else
|
25
|
+
p.comm =~ /#{comm_or_pid}/
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
|
31
|
+
def initialize(args)
|
32
|
+
return unless args[0]
|
33
|
+
@user, @pid, @cpu, @mem, @vsz, @rss, @tty, @stat, @start, @time, *@comm = args
|
34
|
+
|
35
|
+
@pid, @vsz, @rss = [@pid, @vsz, @rss].map(&:to_i)
|
36
|
+
@cpu, @mem = [@cpu, @mem].map(&:to_f)
|
37
|
+
@comm = @comm.join(" ")
|
38
|
+
end
|
39
|
+
|
40
|
+
def hup!
|
41
|
+
exec "kill -1 #{pid}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def kill!
|
45
|
+
exec "kill #{pid}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def move_to_acre!
|
49
|
+
exec "kill -9 #{pid}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_s
|
53
|
+
"#{pid} - #{user} - #{comm} (#{mem}, #{cpu})"
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.genocide!(ary)
|
57
|
+
parsed = ary.gsub(/\W/, "").split(" ")
|
58
|
+
for prok in parsed
|
59
|
+
find(prok).each(&:kill!)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
protected
|
64
|
+
|
65
|
+
def exec(comm)
|
66
|
+
puts "[PROK] exec => #{comm}"
|
67
|
+
`#{comm}`
|
68
|
+
end
|
69
|
+
|
70
|
+
def force(f)
|
71
|
+
{ :hup => "-1", :die => "-9"}[f]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
# COMM = {
|
78
|
+
# :top => "top -n 1",
|
79
|
+
# }
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
# def top
|
84
|
+
# info, tasks, cpus, mem, swap, n, n, *rest = *@res[:top]
|
85
|
+
# n, total, used, free, buffered = *mem.match(/(\d*)k\D*(\d*)k\D*(\d*)k\D*(\d*)k.*/)
|
86
|
+
# cached = swap.match(/(\d*)k cached/)[0]
|
87
|
+
# proks = rest.map do |r|
|
88
|
+
# r.split(" ")
|
89
|
+
# end
|
90
|
+
|
91
|
+
# # fail... top don't show a good stat....
|
92
|
+
# @proks = proks.reject do |p|
|
93
|
+
# p[0] == nil || Prok::BANLIST.select{ |pl| pl =~ p[11] }[0]
|
94
|
+
# end
|
95
|
+
# rescue => e
|
96
|
+
# nil
|
97
|
+
# end
|