Changeset 871 for trunk/Monitoring/Watch


Ignore:
Timestamp:
06/11/13 09:33:58 (11 years ago)
Author:
jripsl
Message:

Improve "config-card" file handling.
Use explicite function for "repo_io" module initialization

Location:
trunk/Monitoring/Watch
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Monitoring/Watch/repo-state

    r866 r871  
    11#!/usr/bin/env python 
    22 
     3# --- ncurses sample 1 
     4 
     5#import curses 
     6#from curses import panel 
     7# 
     8#class Menu(object): 
     9# 
     10#       def __init__(self, items, stdscreen): 
     11#               self.window = stdscreen.subwin(0,0) 
     12#               self.window.keypad(1) 
     13#               self.panel = panel.new_panel(self.window) 
     14#               self.panel.hide() 
     15#               panel.update_panels() 
     16# 
     17#               self.position = 0 
     18#               self.items = items 
     19#               self.items.append(('exit','exit')) 
     20# 
     21#       def navigate(self, n): 
     22#               self.position += n 
     23#               if self.position < 0: 
     24#                       self.position = 0 
     25#               elif self.position >= len(self.items): 
     26#                       self.position = len(self.items)-1 
     27# 
     28#       def display(self): 
     29#               self.panel.top() 
     30#               self.panel.show() 
     31#               self.window.clear() 
     32# 
     33# 
     34#               while True: 
     35#                       self.window.refresh() 
     36#                       curses.doupdate() 
     37#                       for index, item in enumerate(self.items): 
     38#                               if index == self.position: 
     39#                                       mode = curses.A_REVERSE 
     40#                               else: 
     41#                                       mode = curses.A_NORMAL 
     42# 
     43#                               msg = '%d. %s' % (index, item[0]) 
     44#                               self.window.addstr(1+index, 1, msg, mode) 
     45# 
     46#                       key = self.window.getch() 
     47# 
     48#                       if key in [curses.KEY_ENTER, ord('\n')]: 
     49#                               if self.position == len(self.items)-1: 
     50#                                       break 
     51#                               else: 
     52#                                       self.items[self.position][1]() 
     53# 
     54#                       elif key == curses.KEY_UP: 
     55#                               self.navigate(-1) 
     56# 
     57#                       elif key == curses.KEY_DOWN: 
     58#                               self.navigate(1) 
     59# 
     60#               self.window.clear() 
     61#               self.panel.hide() 
     62#               panel.update_panels() 
     63#               curses.doupdate() 
     64# 
     65# 
     66#class MyApp(object): 
     67# 
     68#       def __init__(self, stdscreen): 
     69#               self.screen = stdscreen 
     70#               curses.curs_set(0) 
     71# 
     72#               submenu_items = [ 
     73#                               ('beep', curses.beep), 
     74#                               ('flash', curses.flash) 
     75#                               ] 
     76#               submenu = Menu(submenu_items, self.screen) 
     77# 
     78#               main_menu_items = [ 
     79#                               ('beep', curses.beep), 
     80#                               ('flash', curses.flash), 
     81#                               ('submenu', submenu.display) 
     82#                               ] 
     83#               main_menu = Menu(main_menu_items, self.screen) 
     84#               main_menu.display() 
     85 
     86#if __name__ == '__main__': 
     87#       curses.wrapper(MyApp) 
     88 
     89 
     90 
     91 
     92 
     93# --- ncurses sample 2 
     94 
     95# Author: Nikolai Tschacher 
     96# Date: 02.06.2013 
     97# 
     98#class BoxSelector: 
     99#    """ Originally designed for accman.py. 
     100#        Display options build from a list of strings in a (unix) terminal. 
     101#        The user can browser though the textboxes and select one with enter. 
     102#    """ 
     103#     
     104#    def __init__(self, L): 
     105#        """ Create a BoxSelector object.  
     106#            L is a list of strings. Each string is used to build  
     107#            a textbox. 
     108#        """ 
     109#        self.L = L 
     110#        # Element parameters. Change them here. 
     111#        self.TEXTBOX_WIDTH = 50 
     112#        self.TEXTBOX_HEIGHT = 6 
     113# 
     114#        self.PAD_WIDTH = 400 
     115#        self.PAD_HEIGHT = 10000 
     116#         
     117#    def pick(self): 
     118#        """ Just run this when you want to spawn the selction process. """ 
     119#        self._init_curses() 
     120#        self._create_pad() 
     121#         
     122#        windows = self._make_textboxes() 
     123#        picked = self._select_textbox(windows) 
     124#         
     125#        self._end_curses() 
     126#         
     127#        return picked 
     128#         
     129#    def _init_curses(self): 
     130#        """ Inits the curses appliation """ 
     131#        # initscr() returns a window object representing the entire screen. 
     132#        self.stdscr = curses.initscr() 
     133#        # turn off automatic echoing of keys to the screen 
     134#        curses.noecho() 
     135#        # Enable non-blocking mode. Keys are read directly, without hitting enter. 
     136#        curses.cbreak() 
     137#        # Disable the mouse cursor. 
     138#        curses.curs_set(0) 
     139#        self.stdscr.keypad(1) 
     140#        # Enable colorous output. 
     141#        curses.start_color() 
     142#        curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN) 
     143#        curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK) 
     144#        self.stdscr.bkgd(curses.color_pair(2)) 
     145#        self.stdscr.refresh() 
     146# 
     147#    def _end_curses(self): 
     148#        """ Terminates the curses application. """ 
     149#        curses.nocbreak() 
     150#        self.stdscr.keypad(0) 
     151#        curses.echo() 
     152#        curses.endwin() 
     153#         
     154#    def _create_pad(self): 
     155#        """ Creates a big self.pad to place the textboxes in. """ 
     156#        self.pad = curses.newpad(self.PAD_HEIGHT, self.PAD_WIDTH) 
     157#        self.pad.box() 
     158#         
     159#    def _make_textboxes(self): 
     160#        """ Build the textboxes in the pad center and put them in the  
     161#            horizontal middle of the pad. """ 
     162#        # Get the actual screensize. 
     163#        maxy, maxx = self.stdscr.getmaxyx() 
     164#         
     165#        windows = [] 
     166#        i = 1 
     167#        for s in self.L: 
     168#            windows.append(self.pad.derwin(self.TEXTBOX_HEIGHT, 
     169#                    self.TEXTBOX_WIDTH, i, self.PAD_WIDTH//2-self.TEXTBOX_WIDTH//2)) 
     170#            i += self.TEXTBOX_HEIGHT 
     171#             
     172#        for k in range(len(windows)): 
     173#            windows[k].box() 
     174#            windows[k].addstr(4, 4, '0x{0:X} - {1}'.format(k, self.L[k])) 
     175#             
     176#        return windows 
     177# 
     178#    def _center_view(self, window): 
     179#        """ Centers and aligns the view according to the window argument given.  
     180#            Returns the (y, x) coordinates of the centered window. """ 
     181#        # The refresh() and noutrefresh() methods of a self.pad require 6 arguments 
     182#        # to specify the part of the self.pad to be displayed and the location on 
     183#        # the screen to be used for the display. The arguments are pminrow, 
     184#        # pmincol, sminrow, smincol, smaxrow, smaxcol; the p arguments refer 
     185#        # to the upper left corner of the self.pad region to be displayed and the 
     186#        # s arguments define a clipping box on the screen within which the 
     187#        # self.pad region is to be displayed. 
     188#        cy, cx = window.getbegyx() 
     189#        maxy, maxx = self.stdscr.getmaxyx() 
     190#        self.pad.refresh(cy, cx, 1, maxx//2 - self.TEXTBOX_WIDTH//2, maxy-1, maxx-1) 
     191#        return (cy, cx) 
     192#         
     193#    def _select_textbox(self, windows): 
     194#        # See at the root textbox. 
     195#        topy, topx = self._center_view(windows[0]) 
     196#         
     197#        current_selected = 0 
     198#        last = 1 
     199#        top_textbox = windows[0] 
     200#         
     201#        while True: 
     202#            # Highligth the selected one, the last selected textbox should 
     203#            # become normal again. 
     204#            windows[current_selected].bkgd(curses.color_pair(1)) 
     205#            windows[last].bkgd(curses.color_pair(2)) 
     206#             
     207#            # While the textbox can be displayed on the page with the current  
     208#            # top_textbox, don't alter the view. When this becomes impossible,  
     209#            # center the view to last displayable textbox on the previous view. 
     210#            maxy, maxx = self.stdscr.getmaxyx() 
     211#            cy, cx = windows[current_selected].getbegyx() 
     212#             
     213#            # The current window is to far down. Switch the top textbox. 
     214#            if ((topy + maxy - self.TEXTBOX_HEIGHT) <= cy): 
     215#                top_textbox = windows[current_selected] 
     216#             
     217#            # The current window is to far up. There is a better way though... 
     218#            if topy >= cy + self.TEXTBOX_HEIGHT: 
     219#                top_textbox = windows[current_selected] 
     220#             
     221#            if last != current_selected: 
     222#                last = current_selected 
     223#                 
     224#            topy, topx = self._center_view(top_textbox) 
     225#             
     226#            c = self.stdscr.getch() 
     227#             
     228#            # Vim like KEY_UP/KEY_DOWN with j(DOWN) and k(UP). 
     229#            if c == ord('j'): 
     230#                if current_selected >= len(windows)-1: 
     231#                    current_selected = 0 # wrap around. 
     232#                else: 
     233#                    current_selected += 1 
     234#            elif c == ord('k'): 
     235#                if current_selected <= 0: 
     236#                    current_selected = len(windows)-1 # wrap around. 
     237#                else: 
     238#                    current_selected -= 1 
     239#            elif c == ord('q'): # Quit without selecting. 
     240#                break 
     241#            # At hitting enter, return the index of the selected list element. 
     242#            elif c == curses.KEY_ENTER or c == 10: 
     243#                return int(current_selected) 
     244# 
     245# 
     246#if __name__ == '__main__': 
     247#    # As simple as that. 
     248#    L = [ 
     249#         'I wish I was a wizard', 
     250#         'Sometimes it all just makes sense', 
     251#         'This string is here because I need it', 
     252#         'Being or not being!', 
     253#         'Python is worse then PHP ;)', 
     254#         'a -> b <=> if a then b' 
     255#        ] 
     256#         
     257#    choice = BoxSelector(L).pick() 
     258#    print('[+] Your choice was "{0}"'.format(L[choice])) 
     259 
     260 
     261 
     262 
     263 
     264 
     265 
     266# --- ncurses sample 3 
     267 
     268 
     269 
     270import sys 
    3271import curses 
    4 from curses import panel 
    5  
    6 class Menu(object): 
    7  
    8         def __init__(self, items, stdscreen): 
    9                 self.window = stdscreen.subwin(0,0) 
    10                 self.window.keypad(1) 
    11                 self.panel = panel.new_panel(self.window) 
    12                 self.panel.hide() 
    13                 panel.update_panels() 
    14  
    15                 self.position = 0 
    16                 self.items = items 
    17                 self.items.append(('exit','exit')) 
    18  
    19         def navigate(self, n): 
    20                 self.position += n 
    21                 if self.position < 0: 
    22                         self.position = 0 
    23                 elif self.position >= len(self.items): 
    24                         self.position = len(self.items)-1 
    25  
    26         def display(self): 
    27                 self.panel.top() 
    28                 self.panel.show() 
    29                 self.window.clear() 
    30  
    31  
    32                 while True: 
    33                         self.window.refresh() 
    34                         curses.doupdate() 
    35                         for index, item in enumerate(self.items): 
    36                                 if index == self.position: 
    37                                         mode = curses.A_REVERSE 
    38                                 else: 
    39                                         mode = curses.A_NORMAL 
    40  
    41                                 msg = '%d. %s' % (index, item[0]) 
    42                                 self.window.addstr(1+index, 1, msg, mode) 
    43  
    44                         key = self.window.getch() 
    45  
    46                         if key in [curses.KEY_ENTER, ord('\n')]: 
    47                                 if self.position == len(self.items)-1: 
    48                                         break 
    49                                 else: 
    50                                         self.items[self.position][1]() 
    51  
    52                         elif key == curses.KEY_UP: 
    53                                 self.navigate(-1) 
    54  
    55                         elif key == curses.KEY_DOWN: 
    56                                 self.navigate(1) 
    57  
    58                 self.window.clear() 
    59                 self.panel.hide() 
    60                 panel.update_panels() 
    61                 curses.doupdate() 
    62  
    63  
    64 class MyApp(object): 
    65  
    66         def __init__(self, stdscreen): 
    67                 self.screen = stdscreen 
    68                 curses.curs_set(0) 
    69  
    70                 submenu_items = [ 
    71                                 ('beep', curses.beep), 
    72                                 ('flash', curses.flash) 
    73                                 ] 
    74                 submenu = Menu(submenu_items, self.screen) 
    75  
    76                 main_menu_items = [ 
    77                                 ('beep', curses.beep), 
    78                                 ('flash', curses.flash), 
    79                                 ('submenu', submenu.display) 
    80                                 ] 
    81                 main_menu = Menu(main_menu_items, self.screen) 
    82                 main_menu.display() 
    83  
    84 if __name__ == '__main__': 
    85         curses.wrapper(MyApp) 
     272#import pprint 
     273 
     274# line below is to include "smon" package in the search path 
     275sys.path.append("/home/jripsl/snapshot/Monitoring") 
     276 
     277from smon import repo_io 
     278import smon.types 
     279 
     280 
     281repo_io.init() # open DB connection 
     282 
     283repo_io.populate_tables_with_sample() 
     284 
     285simulations=repo_io.retrieve_simulations() 
     286 
     287mylines = ["Line {0} ".format(id)*3 for id in range(1,11)] 
     288 
     289 
     290def main(stdscr): 
     291  hlines = begin_y = begin_x = 5 ; wcols = 10 
     292  # calculate total content size 
     293  padhlines = len(mylines) 
     294  padwcols = 0 
     295  for line in mylines: 
     296    if len(line) > padwcols: padwcols = len(line) 
     297  padhlines += 2 ; padwcols += 2 # allow border 
     298  #stdscr.addstr("padhlines "+str(padhlines)+" padwcols "+str(padwcols)+"; ") 
     299  # both newpad and subpad are <class '_curses.curses window'>: 
     300  mypadn = curses.newpad(padhlines, padwcols) 
     301  mypads = stdscr.subpad(padhlines, padwcols, begin_y, begin_x+padwcols+4) 
     302  #stdscr.addstr(str(type(mypadn))+" "+str(type(mypads)) + "\n") 
     303  mypadn.scrollok(1) 
     304  mypadn.idlok(1) 
     305  mypads.scrollok(1) 
     306  mypads.idlok(1) 
     307 
     308  mypadn.border(0) # first ... 
     309  mypads.border(0) # ... border 
     310 
     311  for line in mylines: 
     312    mypadn.addstr(padhlines-1,1, line) 
     313    mypadn.scroll(1) 
     314    mypads.addstr(padhlines-1,1, line) 
     315    mypads.scroll(1) 
     316 
     317  mypadn.border(0) # second ... 
     318  mypads.border(0) # ... border 
     319 
     320  # refresh parent first, to render the texts on top 
     321  #~ stdscr.refresh() 
     322 
     323  # refresh the pads next 
     324  mypadn.refresh(0,0, begin_y,begin_x, begin_y+hlines, begin_x+padwcols) 
     325  mypads.refresh() 
     326  mypads.touchwin() 
     327  mypadn.touchwin() 
     328  stdscr.touchwin() # no real effect here 
     329  #stdscr.refresh() # not here! overwrites newpad! 
     330  mypadn.getch() 
     331  # even THIS command erases newpad! 
     332  # (unless stdscr.refresh() previously): 
     333  stdscr.getch() 
     334 
     335curses.wrapper(main) 
     336 
     337 
     338 
     339 
     340repo_io.free() # close DB connection 
     341 
     342 
     343 
     344 
     345 
     346 
     347 
     348 
     349 
     350# --- urwid sample 1 
     351 
     352#import urwid 
     353# 
     354#txt = urwid.Text(u"Hello World") 
     355#fill = urwid.Filler(txt, 'top') 
     356#loop = urwid.MainLoop(fill) 
     357#loop.run() 
     358 
     359 
     360# --- urwid sample 2 
     361 
     362#import urwid 
     363# 
     364#def show_or_exit(key): 
     365#    if key in ('q', 'Q'): 
     366#        raise urwid.ExitMainLoop() 
     367#    txt.set_text(repr(key)) 
     368# 
     369#txt = urwid.Text(u"Hello World") 
     370#fill = urwid.Filler(txt, 'top') 
     371#loop = urwid.MainLoop(fill, unhandled_input=show_or_exit) 
     372#loop.run() 
     373 
  • trunk/Monitoring/Watch/watch

    r866 r871  
    2929import smon.types 
    3030 
    31 """ 
    32 Code list reminder 
    33  
    34 0000 (la simulation démarre) 
    35 1000 (le job d'une simulation démarre) 
    36 2000 (PushStack) 
    37 3000 (PopStack OK) 
    38 9000 (PopStack NOK) 
    39 9999 (FATAL) 
    40 """ 
    41  
    4231class Mail(): 
    4332 
     
    129118        @classmethod 
    130119        def start(cls): 
    131                 pass 
     120                repo_io.init() # open DB connection 
    132121 
    133122        @classmethod 
    134123        def stop(cls): 
    135                 pass 
     124                repo_io.free() # close DB connection 
    136125 
    137126        @classmethod 
     
    174163 
    175164                                # debug 
    176                                 print " [x] Received %s" % field 
     165                                #print " [x] Received %s" % field 
    177166 
    178167                                splitted_field=field.split(":") 
     
    185174 
    186175                        # debug 
    187                         print " [x] Received %s (encoded)" % l__tmp_dic["body"] 
     176                        #print " [x] Received %s (encoded)" % l__tmp_dic["body"] 
    188177 
    189178                         
     
    194183                        # debug 
    195184                        #print " [x] Received %s" % raw_msg 
    196                         print " [x] Received %s (uudecoded)" % base64_decoded_msg  
    197185                        #print " [x] Received %s (uudecoded)" % base64_decoded_msg  
    198  
    199  
     186                        #print " [x] Received %s (uudecoded)" % base64_decoded_msg  
     187 
     188 
     189                        # message deserialization  
    200190                        message=None 
    201191                        try: 
    202                                 # body deserialization  
    203192                                JSON_msg=json.loads(base64_decoded_msg) 
    204193                                message=smon.types.Message(JSON_msg)      # all JSON object members will be available in smon.types.Message object 
    205194 
    206                                 if "file" in l__tmp_dic: 
    207  
    208                                         # base64 decode file 
    209                                         base64_decoded_file=base64.b64decode(l__tmp_dic["file"]) 
    210  
    211                                         # add into msg 
    212                                         message.file=base64_decoded_file 
    213  
     195 
     196 
     197                        except Exception,e: 
     198                                print "ERR009 - exception occurs (exception=%s,msg=%s)"%(str(e),base64_decoded_msg) 
     199 
     200                                #traceback.print_exc() 
     201                                #raise 
     202 
     203 
     204 
     205                        # manage config-card file which is attached to the "0000" type message (this file is base64 encoded and need to be unencoded) 
     206                        # 
     207                        if "file" in l__tmp_dic: 
     208 
     209                                # base64 decode file 
     210                                base64_decoded_file=base64.b64decode(l__tmp_dic["file"]) 
     211 
     212                                # add as msg attribute 
     213                                message.file=base64_decoded_file 
     214 
     215 
     216 
     217                        # execute actions 
     218                        try: 
    214219                                # message code based action 
    215                                 Actions.execActions(message) 
    216  
     220                                #Actions.execActions(message) 
     221                                pass 
    217222                        except Exception,e: 
    218                                 print "Exception occurs (exception=%s,msg=%s)"%(str(e),base64_decoded_msg) 
    219  
    220                                 #traceback.print_exc() 
     223                                print "ERR019 - exception occurs (exception=%s,msg=%s)"%(str(e),base64_decoded_msg) 
     224 
     225                                traceback.print_exc() 
    221226 
    222227                                raise 
     228 
     229 
    223230 
    224231                self.channel.basic_consume(callback, queue='myqueue', no_ack=True) 
Note: See TracChangeset for help on using the changeset viewer.