]>
git.immae.eu Git - perso/Immae/Projets/Ruby/Monitor.git/blob - monitor.rb
13 buffer
<< read_nonblock(1) while buffer
[-1] !
= "\n"
15 rescue IO
::WaitReadable => blocking
16 if (not buffer
.empty
?)
26 options
= OpenStruct
.new()
29 opt_parser
= OptionParser
.new() do |opts
|
31 opts
.banner
= "Usage: monitor [options]"
34 opts
.on( '-f', "-f ini_file", "chose a different initialization file") do |ini
|
38 opt_parser
.parse!
(args
)
44 def initialize(inifile
)
46 @win = Ncurses
::WINDOW.new(0, Ncurses
.COLS()/4, 0, 0)
70 def print_list(entry
=nil)
75 @params.each_section
do |section
|
77 @win.attron(Ncurses
::A_REVERSE)
80 print_line(@win,@params[section
]['Name'])
82 @win.attroff(Ncurses
::A_REVERSE)
94 def initialize(winsize
,winpos
,params
)
96 @win = Ncurses
::WINDOW.new(0, winsize
, 0, winpos
)
97 @panel = Ncurses
::Panel::PANEL.new(@win)
98 if(@params['Type'] == 'oneshot')
99 @buffer = Buffer
.new(0)
101 @buffer = Buffer
.new(@params['Buffer'].to_i
)
112 def move_resize(winsize
,winpos
)
113 newwin
= Ncurses
::WINDOW.new(0, winsize
, 0, winpos
)
114 Ncurses
::Panel.replace_panel(@panel, newwin
)
120 @last_update = Time
.now
121 @date = @last_update.strftime("%F %R:%S")
125 @win.border(*([0]*8))
130 @win.addnstr(@params['Name'],@win.getmaxx-
@date.length-10
)
131 @win.move(0,@win.getmaxx-
@date.length-2
)
142 Ncurses
::Panel.top_panel(@panel)
143 Ncurses
::Panel.update_panels
146 def hscroll(scroll
=0)
151 if(@hscroll > @buffer.maxlen()-@win.getmaxx+
3)
152 @hscroll = @buffer.maxlen()-@win.getmaxx+
3
157 def scroll(scroll
=0,goto
=nil,fact
=nil)
159 scroll
= (fact
* @win.getmaxy
.to_f
).to_i
160 elsif (not goto
.nil?)
161 @curr_offset = (goto
* @buffer.size()).to_i
164 #@curr_offset -= @win.getmaxy/2
165 @curr_offset +
= scroll
169 if(@curr_offset > @buffer.size()-@win.getmaxy+
2)
170 @curr_offset = @buffer.size()-@win.getmaxy+
2
180 @buffer.yield(@win.getmaxy-2
,@curr_offset) { |l
,type
|
182 if(type
== 1) then @win.attron(Ncurses
.COLOR_PAIR(1)) end
183 print_line(@win,l
,hscroll
=@hscroll)
184 if(type
== 1) then @win.attroff(Ncurses
.COLOR_PAIR(1)) end
187 if(@buffer.has_before
?)
188 @win.move(2,@win.getmaxx-1
)
189 @win.attron(Ncurses
::A_REVERSE)
191 @win.attroff(Ncurses
::A_REVERSE)
193 if(@buffer.has_after
?)
194 @win.move(@win.getmaxy-2
,@win.getmaxx-1
)
195 @win.attron(Ncurses
::A_REVERSE)
197 @win.attroff(Ncurses
::A_REVERSE)
204 @buffer.push(@proc.readline_nonblock
)
207 rescue IO
::WaitReadable
211 def update(force
=false)
212 if(@params['Type'] == 'continuous')
215 if(@params['Type'] == 'oneshot')
216 if(force
or (Time
.now
- @last_update > @params['Periodic'].to_i
))
226 if(@params['Type'] == 'oneshot')
228 Open3
.popen3(@params["Command"]) { |i
,o
,e
,t
|
229 while ((not o
.eof
?) or (not e
.eof
?))
230 rs
= IO
.select([o
,e
],nil)[0]
231 r
= (rs
[0].eof
?)? rs
[1] : rs
[0]
233 if r
.fileno
== o
.fileno
234 @buffer.push(r
.readline
)
235 elsif r
.fileno
== e
.fileno
236 @buffer.push(r
.readline
,type
=1)
240 elsif(@params['Type'] == 'continuous')
241 @proc = IO
.popen(@params["Command"])
262 @buff.each
do |string
|
263 if string
.length
> maxlen
264 maxlen
= string
.length
270 def push(string
,type
=0)
271 if(string
.chomp
.empty
?) then string
= " " end
272 string
.split( /\r?\n/ ).each
do |line
|
273 @buff[@current] = line
274 @buff_type[@current] = type
276 @current = (1+
@current) % @size
278 @current = 1+
@current
280 if(@current == 0) then @wrap = true end
283 def yield(size
,offset
=0,&block
)
284 if(size
< 0) then size
= 0 end
285 range
= Range
.new(0,@current-1).to_a
287 range
= Range
.new(@current,@size-1).to_a + range
289 range
= range
.last(size+offset
)[0,size
]
290 @before = (size+offset
< @buff.length
)
291 @after = (offset !
= 0 and size
< @buff.length
)
294 yield [@buff[i
],@buff_type[i
]]
297 return range
.collect
{|r
| [@buff[r
],@buff_type[r
]]}
313 def find_ini(option_inifile
)
314 if(not option_inifile
.nil?)
315 inifile
= option_inifile
316 elsif(ENV.has_key
?('MONITOR_RC'))
317 inifile
= ENV['MONITOR_RC']
318 elsif(Process
.uid
== 0)
319 inifile
= "/etc/monitor.rc"
321 inifile
= ENV['HOME']+
"/.monitorrc"
327 inifile
= IniFile
.load(ini
)
329 puts
"Initialization file not found or not readable"
335 def print_line(win
, str
, hscroll
=0)
337 str
[0,5].match(/\033\[3(.)m/) { |c
| #Line starts with an escape sequence. We handle that `a la xterm`
338 Ncurses
.init_pair(10, c
[1].to_i
, Ncurses
::COLOR_BLACK)
339 win
.attron(Ncurses
.COLOR_PAIR(10))
341 str
= str
[5,str
.length
]
343 str
= str
.gsub("\011"," ")
344 #Any other control char is ignored and escaped
345 str
= str
.gsub(/[[:cntrl:]]/) { |m
|
349 strcut
= str
[hscroll
,str
.length
]
350 if(strcut
.nil? or strcut
.empty
?)
357 winlen
= win
.getmaxx-win
.getcurx-1
359 win
.addstr(str +
" "*(winlen-strlen
))
361 win
.addstr(str
[0,winlen-1
]+
"…")
364 win
.attroff(Ncurses
.COLOR_PAIR(10))
368 def make_bufwins(inifile
)
370 inifile
.each_section
do |section
|
371 bufwin
= Buff_Win
.new(Ncurses
.COLS()-Ncurses
.COLS()/4,
379 def update_buffers(bufwins
)
380 bufwins
.each
do |bufwin
|
385 def redraw_all(list
,bufwins
,curr_bufwin
)
386 bufwins
.each
do |bufwin
|
387 bufwin
.move_resize(Ncurses
.COLS()-Ncurses
.COLS()/4,Ncurses.COLS()/4)
389 list
.resize(Ncurses
.LINES(), Ncurses
.COLS()/4)
393 curr_bufwin
.refresh()
397 options
= OptParse
.new().parse(ARGV)
398 inifile
= read_ini(find_ini(options
.inifile
))
403 Ncurses
.cbreak
# provide unbuffered input
404 Ncurses
.noecho
# turn off input echoing
405 #Ncurses.nonl # turn off newline translation
406 #Ncurses.stdscr.intrflush(false) # turn off flush-on-interrupt
407 Ncurses
.stdscr
.keypad(true) # turn on keypad mode
408 Ncurses
.init_pair(1, Ncurses
::COLOR_RED, Ncurses
::COLOR_BLACK)
409 Ncurses
.init_pair(10, Ncurses
::COLOR_WHITE, Ncurses
::COLOR_BLACK)
412 list
= List_Win
.new(inifile
)
413 bufwins
= make_bufwins(inifile
)
415 cur_bufwin
= bufwins
[entry
]
416 cur_bufwin
.show_win()
418 while(ch
= list
.getch()) do
421 entry
= (entry +
1) % bufwins
.length
422 cur_bufwin
= bufwins
[entry
]
423 cur_bufwin
.show_win()
424 list
.print_list(entry
=entry
)
426 entry
= (entry
-1) % bufwins
.length
427 cur_bufwin
= bufwins
[entry
]
428 cur_bufwin
.show_win()
429 list
.print_list(entry
=entry
)
431 redraw_all(list
,bufwins
,cur_bufwin
)
433 cur_bufwin
.update(force
=true)
434 when Ncurses
::KEY_RESIZE
435 redraw_all(list
,bufwins
,cur_bufwin
)
436 when Ncurses
::KEY_LEFT
437 cur_bufwin
.hscroll(scroll
=-1)
438 when Ncurses
::KEY_RIGHT
439 cur_bufwin
.hscroll(scroll
=1)
440 when Ncurses
::KEY_DOWN
441 cur_bufwin
.scroll(scroll
=-1)
443 cur_bufwin
.scroll(scroll
=1)
444 when Ncurses
::KEY_NPAGE
445 cur_bufwin
.scroll(scroll
=0,goto
=nil,fact
=-0.75)
446 when Ncurses
::KEY_PPAGE
447 cur_bufwin
.scroll(scroll
=0,goto
=nil,fact
=0.75)
448 when Ncurses
::KEY_HOME
449 cur_bufwin
.scroll(scroll
=0,goto
=1.0)
450 when Ncurses
::KEY_END
451 cur_bufwin
.scroll(scroll
=0,goto
=0.0)
453 update_buffers(bufwins
)
454 cur_bufwin
.show_win()