diff options
Diffstat (limited to 'monitor.rb')
-rw-r--r-- | monitor.rb | 272 |
1 files changed, 2 insertions, 270 deletions
@@ -6,6 +6,8 @@ require "inifile" | |||
6 | require "open3" | 6 | require "open3" |
7 | require 'optparse' | 7 | require 'optparse' |
8 | require 'ostruct' | 8 | require 'ostruct' |
9 | require_relative 'buffer' | ||
10 | require_relative 'windows' | ||
9 | 11 | ||
10 | class IO | 12 | class IO |
11 | def readline_nonblock | 13 | def readline_nonblock |
@@ -40,276 +42,6 @@ class OptParse | |||
40 | end | 42 | end |
41 | end | 43 | end |
42 | 44 | ||
43 | class List_Win | ||
44 | def initialize(inifile) | ||
45 | @params = inifile | ||
46 | @win = Ncurses::WINDOW.new(0, Ncurses.COLS()/4, 0, 0) | ||
47 | @win.border(*([0]*8)) | ||
48 | @win.keypad(true) | ||
49 | @win.timeout(1000) | ||
50 | @win.refresh() | ||
51 | @entry = 0 | ||
52 | end | ||
53 | |||
54 | def getch() | ||
55 | @win.getch() | ||
56 | end | ||
57 | def clear() | ||
58 | @win.clear() | ||
59 | end | ||
60 | def resize(x,y) | ||
61 | @win.resize(x,y) | ||
62 | end | ||
63 | def move(x,y) | ||
64 | @win.move(x,y) | ||
65 | end | ||
66 | def refresh() | ||
67 | @win.refresh() | ||
68 | end | ||
69 | |||
70 | def print_list(entry=nil) | ||
71 | if(not entry.nil?) | ||
72 | @entry = entry | ||
73 | end | ||
74 | i = 0 | ||
75 | @params.each_section do |section| | ||
76 | if(@entry == i) | ||
77 | @win.attron(Ncurses::A_REVERSE) | ||
78 | end | ||
79 | @win.move(i+1,1) | ||
80 | print_line(@win,@params[section]['Name']) | ||
81 | if(@entry == i) | ||
82 | @win.attroff(Ncurses::A_REVERSE) | ||
83 | end | ||
84 | i = i+1 | ||
85 | end | ||
86 | @win.border(*([0]*8)) | ||
87 | @win.move(0,3) | ||
88 | @win.addstr("Menu") | ||
89 | @win.refresh() | ||
90 | end | ||
91 | end | ||
92 | |||
93 | class Buff_Win | ||
94 | def initialize(winsize,winpos,params) | ||
95 | @params = 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) | ||
100 | else | ||
101 | @buffer = Buffer.new(@params['Buffer'].to_i) | ||
102 | end | ||
103 | @proc = nil | ||
104 | @curr_offset = 0 | ||
105 | @hscroll = 0 | ||
106 | update_date() | ||
107 | |||
108 | spawn_proc() | ||
109 | print_buffer() | ||
110 | end | ||
111 | |||
112 | def move_resize(winsize,winpos) | ||
113 | newwin = Ncurses::WINDOW.new(0, winsize, 0, winpos) | ||
114 | Ncurses::Panel.replace_panel(@panel, newwin) | ||
115 | Ncurses.delwin(@win) | ||
116 | @win = newwin | ||
117 | print_buffer() | ||
118 | end | ||
119 | def update_date() | ||
120 | @last_update = Time.now | ||
121 | @date = @last_update.strftime("%F %R:%S") | ||
122 | end | ||
123 | |||
124 | def win_border() | ||
125 | @win.border(*([0]*8)) | ||
126 | win_header() | ||
127 | end | ||
128 | def win_header() | ||
129 | @win.move(0,3) | ||
130 | @win.addnstr(@params['Name'],@win.getmaxx-@date.length-10) | ||
131 | @win.move(0,@win.getmaxx-@date.length-2) | ||
132 | @win.addstr(@date) | ||
133 | end | ||
134 | def refresh() | ||
135 | @win.refresh() | ||
136 | end | ||
137 | def clear() | ||
138 | @win.clear() | ||
139 | end | ||
140 | |||
141 | def show_win() | ||
142 | Ncurses::Panel.top_panel(@panel) | ||
143 | Ncurses::Panel.update_panels | ||
144 | end | ||
145 | |||
146 | def hscroll(scroll=0) | ||
147 | @hscroll += scroll | ||
148 | if(@hscroll < 0) | ||
149 | @hscroll = 0 | ||
150 | end | ||
151 | if(@hscroll > @buffer.maxlen()-@win.getmaxx+3) | ||
152 | @hscroll = @buffer.maxlen()-@win.getmaxx+3 | ||
153 | end | ||
154 | print_buffer() | ||
155 | refresh() | ||
156 | end | ||
157 | def scroll(scroll=0,goto=nil,fact=nil) | ||
158 | if (not fact.nil?) | ||
159 | scroll = (fact * @win.getmaxy.to_f).to_i | ||
160 | elsif (not goto.nil?) | ||
161 | @curr_offset = (goto * @buffer.size()).to_i | ||
162 | scroll = 0 | ||
163 | end | ||
164 | #@curr_offset -= @win.getmaxy/2 | ||
165 | @curr_offset += scroll | ||
166 | if(@curr_offset < 0) | ||
167 | @curr_offset = 0 | ||
168 | end | ||
169 | if(@curr_offset > @buffer.size()-@win.getmaxy+2) | ||
170 | @curr_offset = @buffer.size()-@win.getmaxy+2 | ||
171 | end | ||
172 | print_buffer() | ||
173 | refresh() | ||
174 | end | ||
175 | |||
176 | def print_buffer() | ||
177 | #clear() | ||
178 | win_border() | ||
179 | j = 1 | ||
180 | @buffer.yield(@win.getmaxy-2,@curr_offset) { |l,type| | ||
181 | @win.move(j,1) | ||
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 | ||
185 | j = j+1 | ||
186 | } | ||
187 | if(@buffer.has_before?) | ||
188 | @win.move(2,@win.getmaxx-1) | ||
189 | @win.attron(Ncurses::A_REVERSE) | ||
190 | @win.addstr("↑") | ||
191 | @win.attroff(Ncurses::A_REVERSE) | ||
192 | end | ||
193 | if(@buffer.has_after?) | ||
194 | @win.move(@win.getmaxy-2,@win.getmaxx-1) | ||
195 | @win.attron(Ncurses::A_REVERSE) | ||
196 | @win.addstr("↓") | ||
197 | @win.attroff(Ncurses::A_REVERSE) | ||
198 | end | ||
199 | end | ||
200 | |||
201 | def proc_readlines() | ||
202 | begin | ||
203 | while true | ||
204 | @buffer.push(@proc.readline_nonblock) | ||
205 | update_date() | ||
206 | end | ||
207 | rescue IO::WaitReadable | ||
208 | end | ||
209 | end | ||
210 | |||
211 | def update(force=false) | ||
212 | if(@params['Type'] == 'continuous') | ||
213 | proc_readlines() | ||
214 | end | ||
215 | if(@params['Type'] == 'oneshot') | ||
216 | if(force or (Time.now - @last_update > @params['Periodic'].to_i)) | ||
217 | @buffer.clear() | ||
218 | spawn_proc() | ||
219 | clear() | ||
220 | end | ||
221 | end | ||
222 | print_buffer() | ||
223 | end | ||
224 | |||
225 | def spawn_proc() | ||
226 | if(@params['Type'] == 'oneshot') | ||
227 | update_date() | ||
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] | ||
232 | |||
233 | if r.fileno == o.fileno | ||
234 | @buffer.push(r.readline) | ||
235 | elsif r.fileno == e.fileno | ||
236 | @buffer.push(r.readline,type=1) | ||
237 | end | ||
238 | end | ||
239 | } | ||
240 | elsif(@params['Type'] == 'continuous') | ||
241 | @proc = IO.popen(@params["Command"]) | ||
242 | proc_readlines() | ||
243 | end | ||
244 | end | ||
245 | end | ||
246 | |||
247 | class Buffer | ||
248 | def initialize(size) | ||
249 | @size = size | ||
250 | @buff = [] | ||
251 | @buff_type = [] | ||
252 | @current = 0 | ||
253 | @wrap = false | ||
254 | @before = false | ||
255 | @after = false | ||
256 | end | ||
257 | def size() | ||
258 | return @buff.length | ||
259 | end | ||
260 | def maxlen() | ||
261 | maxlen = 0 | ||
262 | @buff.each do |string| | ||
263 | if string.length > maxlen | ||
264 | maxlen = string.length | ||
265 | end | ||
266 | end | ||
267 | return maxlen | ||
268 | end | ||
269 | |||
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 | ||
275 | if(@size > 0) | ||
276 | @current = (1+@current) % @size | ||
277 | else | ||
278 | @current = 1+@current | ||
279 | end | ||
280 | if(@current == 0) then @wrap = true end | ||
281 | end | ||
282 | 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 | ||
286 | if(@wrap) | ||
287 | range = Range.new(@current,@size-1).to_a + range | ||
288 | end | ||
289 | range = range.last(size+offset)[0,size] | ||
290 | @before = (size+offset < @buff.length) | ||
291 | @after = (offset != 0 and size < @buff.length) | ||
292 | if(block) | ||
293 | range.each do |i| | ||
294 | yield [@buff[i],@buff_type[i]] | ||
295 | end | ||
296 | else | ||
297 | return range.collect{|r| [@buff[r],@buff_type[r]]} | ||
298 | end | ||
299 | end | ||
300 | def has_after?() | ||
301 | return @after | ||
302 | end | ||
303 | def has_before?() | ||
304 | return @before | ||
305 | end | ||
306 | def clear() | ||
307 | @current = 0 | ||
308 | @buff = [] | ||
309 | @buff_type = [] | ||
310 | end | ||
311 | end | ||
312 | |||
313 | def find_ini(option_inifile) | 45 | def find_ini(option_inifile) |
314 | if(not option_inifile.nil?) | 46 | if(not option_inifile.nil?) |
315 | inifile = option_inifile | 47 | inifile = option_inifile |