Changeset 871 for trunk/Monitoring


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
Files:
12 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) 
  • trunk/Monitoring/libIGCM_mock/libIGCM_mock.sh

    r866 r871  
    1818# init 
    1919 
    20 stack_file=$1 
    2120send_msg_cmd="/home/jripsl/snapshot/Monitoring/CNClient/sendAMQPMsg" 
    22 scenario_dir=scenario 
     21g__stackfile= 
     22g__scenario_dir=scenario 
    2323g__mode="scenario" 
    24 g__stackfile= 
    25 g__scenario_file= 
     24g__scenariofile= 
     25g__dryrun=0 
     26g__confirm=0 
     27g__delay=1 # delay between message 
    2628 
    2729# func 
     
    4749 
    4850OPTIONS: 
     51   -h              this help 
    4952   -f              set stack file 
    5053   -l              print scenarios list  
    51    -m              set mode (if missing, "scenario" mode is used) 
    52    -s              set scenario 
     54   -m              set MODE 
     55                   MODE may be "scenario", "stackfile" or "purge" 
     56                   default mode is "scenario" 
     57   -s              set scenario file 
    5358 
    5459EXAMPLES: 
     
    7479        echo "Scenarios list:" 
    7580        echo "" 
    76         ls -1 $scenario_dir 
     81        ls -1 $g__scenario_dir 
    7782        echo "" 
    7883        exit 2 
     
    8792# parse args 
    8893 
    89 while getopts 'f:hlm:s:' OPTION 
     94while getopts 'cdf:hlm:s:' OPTION 
    9095do 
    9196  case $OPTION in 
     97  c)    g__confirm="1" 
     98        ;; 
     99  d)    g__dryrun="1" 
     100        ;; 
    92101  f)    g__stackfile="$OPTARG" 
    93102        ;; 
     
    98107  m)    g__mode="$OPTARG" 
    99108        ;; 
    100   s)    l__scenario_file="$OPTARG" 
    101  
    102                 if [[ "$l__scenario_file" =~ "*/*" ]]; then 
     109  s)    l__scenariofile="$OPTARG" 
     110 
     111                if [[ "$l__scenariofile" =~ "*/*" ]]; then 
    103112                        # full/relative path was given with the filename 
    104113 
    105                         g__scenario_file="$l__scenario_file" 
     114                        g__scenariofile="$l__scenariofile" 
    106115                else 
    107116                        # only the filename was given 
    108117 
    109                         g__scenario_file="$scenario_dir/$l__scenario_file" 
     118                        g__scenariofile="$g__scenario_dir/$l__scenariofile" 
    110119                fi 
    111120 
     
    120129 
    121130        # check 
    122         if [ ! -f "$g__scenario_file" ]; then 
     131        if [ ! -f "$g__scenariofile" ]; then 
    123132                msg "LIBIGCM-MOCK-ERR003" "scenario file not found" 
    124133                exit 1 
    125134        fi 
    126135 
    127         N=0 
    128         while read LINE ; do 
    129  
    130                 N=$((N+1)) 
     136        while read LINE <&3; do 
     137 
     138                # debug 
     139                #echo $LINE 
    131140 
    132141                l__JSON_msg_buf= 
    133  
    134                 # process fields (split on " " delimiter) 
    135                 for field in "$LINE"; do 
    136                         arr=(${LINE//=/ }) # process key/value (split on "=" delimiter) 
    137                         key=${arr[0]} 
    138                         val=${arr[1]} 
    139  
    140                         # base64 encoding 
     142                N=1 
     143                fields_arr=(${LINE// / }) # process fields (split on " " delimiter) 
     144                for FIELD in "${fields_arr[@]}"; do 
     145 
     146                        # debug  
     147                        #echo $FIELD 
     148 
     149 
     150                        field_arr=(${FIELD//=/ }) # process key/value (split on "=" delimiter) 
     151                        key=${field_arr[0]} 
     152 
     153 
     154                        # HACK 
     155                        if [ "$key" = "file" ]; then 
     156                                # special processing for "file" key (base64 encoding) 
     157 
     158                                l__configcard_file="/home/jripsl/snapshot/Monitoring/sample/${field_arr[1]}" 
     159 
     160                                # check 
     161                                if [ ! -f "$l__configcard_file" ]; then 
     162                                        msg "LIBIGCM-MOCK-ERR004" "config-card file not found ($l__configcard_file)" 
     163                                        exit 1 
     164                                fi 
     165 
     166                                val=$( cat $l__configcard_file | base64 -w 0 ) 
     167 
     168                        else 
     169                                val=${field_arr[1]} 
     170                        fi 
     171 
    141172 
    142173                        # append to JSON message buffer 
    143                         l__JSON_msg_buf=$l__JSON_msg_buf 
    144  
     174                        if [ "$N" -gt "1" ]; then 
     175 
     176                                l__JSON_msg_buf="$l__JSON_msg_buf,\"$key\":\"$val\"" 
     177                        else 
     178                                # first field 
     179 
     180                                l__JSON_msg_buf="\"$key\":\"$val\"" 
     181                        fi 
     182 
     183 
     184                        N=$((N+1)) 
    145185                done 
    146186 
     187                # enclose 
     188                l__JSON_msg_buf="{""$l__JSON_msg_buf""}" 
     189 
     190                # debug 
     191                #echo $l__JSON_msg_buf 
     192 
    147193                # message base64 encoding 
    148  
    149  
    150                 #callname=$(echo $line | awk -F" " '{print $4}' ) 
     194                l__JSON_msg_buf_encoded=$( echo $l__JSON_msg_buf | base64 -w 0 ) 
     195 
     196                # debug 
     197                #echo $l__JSON_msg_buf_encoded 
     198                #echo $send_msg_cmd -h localhost -p 5672 -b "$l__JSON_msg_buf_encoded" 
    151199 
    152200                # send AMQP message 
    153                 #$send_msg_cmd localhost 5672 string "$callname" 
    154                 echo 
    155  
    156         done < $g__scenario_file 
     201                if [ "$g__dryrun" = "1" ]; then 
     202 
     203                        echo $send_msg_cmd -h localhost -p 5672 -b "$l__JSON_msg_buf" 
     204 
     205 
     206                        # debug 
     207                        # 
     208                        # uncomment line below (and comment line above) to output only the encoded message 
     209                        #  
     210                        # it can then be unencoded for debug purpose (message encoding level, not config-card encoding) using command below 
     211                        # ./libIGCM_mock.sh -s start_simu__stop_simu -d | base64 -d | less 
     212                        #  
     213                        #  
     214                        #echo $l__JSON_msg_buf_encoded 
     215 
     216                else 
     217                        $send_msg_cmd -h localhost -p 5672 -b "$l__JSON_msg_buf_encoded" 
     218                fi 
     219 
     220 
     221 
     222 
     223                if [ "$g__confirm" = "1" ]; then 
     224                        read -p "Press enter for next message" bla 
     225                else 
     226                        sleep $g__delay 
     227                fi 
     228 
     229 
     230 
     231                # debug 
     232                #break 
     233 
     234        done 3<$g__scenariofile 
     235 
     236elif [ "$g__mode" = "purge" ]; then 
     237 
     238        : 
    157239 
    158240elif [ "$g__mode" = "stackfile" ]; then 
    159241 
    160242        # check 
    161         if [ ! -f $stack_file ]; then 
     243        if [ ! -f $g__stackfile ]; then 
    162244                msg "LIBIGCM-MOCK-ERR001" "file not found" 
    163245                exit 1 
     
    165247 
    166248        IFS=$'\n' 
    167         for line in $(cat $stack_file); do 
     249        for line in $(cat $g__stackfile); do 
    168250                #echo $line | awk -F" " '{print $4}' 
    169251                callname=$(echo $line | awk -F" " '{print $4}' ) 
  • trunk/Monitoring/libIGCM_mock/scenario/start_simu__fatal_error__stop_simu

    r866 r871  
    1 simuid=simu-001 jobid=job-001 code=0000 file=./sample/config.card.base64 
     1simuid=simu-001 jobid=job-001 code=0000 file=config.card.base64 
    22simuid=simu-001 jobid=job-001 code=1000 
    33simuid=simu-001 jobid=job-001 code=2000 
  • trunk/Monitoring/libIGCM_mock/scenario/start_simu__nonfatal_errors__stop_simu

    r866 r871  
    1 simuid=simu-001 jobid=job-001 code=0000 file=./sample/config.card.base64 
     1simuid=simu-001 jobid=job-001 code=0000 file=config.card.base64 
    22simuid=simu-001 jobid=job-001 code=1000 
    33simuid=simu-001 jobid=job-001 code=2000 
  • trunk/Monitoring/libIGCM_mock/scenario/start_simu__simu_killed

    r866 r871  
    1 simuid=simu-001 jobid=job-001 code=0000 file=./sample/config.card.base64 
     1simuid=simu-001 jobid=job-001 code=0000 file=config.card.base64 
    22simuid=simu-001 jobid=job-001 code=1000 
    33simuid=simu-001 jobid=job-001 code=2000 
  • trunk/Monitoring/libIGCM_mock/scenario/start_simu__simu_segfault

    r866 r871  
    1 simuid=simu-001 jobid=job-001 code=0000 file=./sample/config.card.base64 
     1simuid=simu-001 jobid=job-001 code=0000 file=config.card.base64 
    22simuid=simu-001 jobid=job-001 code=1000 
    33simuid=simu-001 jobid=job-001 code=2000 
  • trunk/Monitoring/libIGCM_mock/scenario/start_simu__start_second_simu_with_same_name

    r866 r871  
    1 simuid=simu-001 jobid=job-001 code=0000 file=./sample/config.card.base64 
     1simuid=simu-001 jobid=job-001 code=0000 file=config.card.base64 
    22simuid=simu-001 jobid=job-001 code=1000 
    33simuid=simu-001 jobid=job-001 code=2000 
  • trunk/Monitoring/libIGCM_mock/scenario/start_simu__stop_simu

    r866 r871  
    1 simuid=simu-001 jobid=job-001 code=0000 file=./sample/config.card.base64 
     1simuid=simu-001 jobid=job-001 code=0000 file=config.card.base64 
    22simuid=simu-001 jobid=job-001 code=1000 
    33simuid=simu-001 jobid=job-001 code=2000 
  • trunk/Monitoring/smon/__init__.py

    r840 r871  
    1111# @license        CeCILL (http://dods.ipsl.jussieu.fr/jripsl/smon/LICENSE) 
    1212################################## 
     13 
     14 
     15# HACK 
     16import sys 
     17sys.path.append("/home/jripsl/snapshot/Monitoring") 
  • trunk/Monitoring/smon/local_repo.py

    r866 r871  
    1414import sys 
    1515import sqlite3 
     16 
     17import types 
    1618 
    1719_conn=None 
     
    3941 
    4042def create_tables(): 
     43 
    4144        _conn.execute("create table simulation (id INTEGER PRIMARY KEY, name TEXT)") 
     45        _conn.execute("create unique index idx_simulation on simulation (name)") 
     46 
    4247        _conn.execute("create table message (id INTEGER PRIMARY KEY, simulation_id TEXT)") # TODO: check how to use INT datatype for simulation_id column 
     48 
     49def populate_tables_with_sample(): 
     50 
     51        rows = [('SIMU-001',), 
     52                        ('SIMU-002',), 
     53                        ('SIMU-003',),] 
     54        _conn.executemany('INSERT INTO simulation (name) VALUES (?)', rows) 
     55 
     56def retrieve_simulations(): 
     57        li=[] 
     58        c=_conn.cursor() 
     59 
     60        c.execute("select name,id from simulation") 
     61 
     62        rs=c.fetchone() 
     63        while rs is not None: 
     64                li.append(types.Simulation(name=rs[0],id=rs[1])) 
     65                rs=c.fetchone() 
     66 
     67        return li 
    4368 
    4469def retrieve_simulation(name): 
     
    5277                raise Exception() 
    5378 
    54         return smon.types.Simulation(id=rs[1]) 
     79        return types.Simulation(id=rs[1]) 
    5580     
    5681def delete_simulation(simulation): 
     
    7297        rs=c.fetchone() 
    7398        while rs is not None: 
    74                 li.append(smon.types.Message(id=rs[0])) 
     99                li.append(types.Message(id=rs[0])) 
    75100                rs=c.fetchone() 
    76101 
     
    93118                raise Exception() 
    94119 
    95         return smon.types.Message(id=rs[0]) 
     120        return types.Message(id=rs[0]) 
  • trunk/Monitoring/smon/repo_io.py

    r866 r871  
    3131 
    3232 
    33 # -- methods -- # 
    34  
    35 def init(): 
    36         if mode==CSTE_MODE_LOCAL_REPO: 
    37                 repo.connect() 
    38         elif mode==CSTE_MODE_REMOTE_REPO: 
    39                 _CONNECTION = "postgresql://postgres:Silence107!@localhost:5432/prodiguer" 
    40                 prodiguer_shared.connect(_CONNECTION) 
    41         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    42                 pass 
    43         else: 
    44                 raise Exception("ERR004 - incorrect mode") 
    45  
    46 def free(): 
    47         if mode==CSTE_MODE_LOCAL_REPO: 
    48                 repo.close() 
    49         elif mode==CSTE_MODE_REMOTE_REPO: 
    50  
    51                 #prodiguer_shared.close() 
    52                 pass 
    53         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    54                 pass 
    55         else: 
    56                 raise Exception("ERR009 - incorrect mode") 
    57  
    58 def test(): 
    59         repo.create_message("test2", 2, "bla2") 
    60         commit() 
    61  
    62         repo.update_simulation_status('1pctCO22', 'ERROR') 
    63         commit() 
    64  
    65         repo.create_message("test3", 3, "bla3") 
    66         rollback() 
    67  
    68 def commit(): 
    69         if mode==CSTE_MODE_LOCAL_REPO: 
    70                 repo.commit() 
    71         elif mode==CSTE_MODE_REMOTE_REPO: 
    72                 elixir.session.commit() 
    73         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    74                 pass 
    75         else: 
    76                 raise Exception("ERR002 - incorrect mode") 
    77  
    78 def rollback(): 
    79         if mode==CSTE_MODE_LOCAL_REPO: 
    80                 repo.rollback() 
    81         elif mode==CSTE_MODE_REMOTE_REPO: 
    82                 elixir.session.rollback() 
    83         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    84                 pass 
    85         else: 
    86                 raise Exception("ERR003 - incorrect mode") 
    87  
    88 def retrieve_simulation(name): 
    89         simulation=None 
    90  
    91         if mode==CSTE_MODE_LOCAL_REPO: 
    92                 simulation=repo.retrieve_simulation(name) 
    93         elif mode==CSTE_MODE_REMOTE_REPO: 
    94  
    95                 # prepare args 
    96                 # .. 
    97  
    98                 # execute 
    99                 s=repo.retrieve_simulation(name) 
    100  
    101                 # process return values 
    102                 simulation=smon.types.Simulation(exec_start_date=s.ExecutionStartDate,exec_end_date=s.ExecutionEndDate,status=s.ExecutionState) # ExecutionState example: EXECUTION_STATE_RUNNING, EXECUTION_STATE_SET.. 
    103  
    104         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    105                 pass 
    106         else: 
    107                 raise Exception("ERR014 - incorrect mode") 
    108  
    109         return simulation 
    110  
    111 def delete_simulation(name): 
    112         if mode==CSTE_MODE_LOCAL_REPO: 
    113                 repo.delete_simulation(name) 
    114         elif mode==CSTE_MODE_REMOTE_REPO: 
    115  
    116                 # prepare args 
    117                 # .. 
    118  
    119                 # execute 
    120                 repo.delete_simulation(name) 
    121  
    122                 # process return values 
    123                 # .. 
    124  
    125         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    126                 pass 
    127         else: 
    128                 raise Exception("ERR015 - incorrect mode") 
    129  
    130 def create_simulation(simulation): 
    131         if mode==CSTE_MODE_LOCAL_REPO: 
    132                 repo.create_simulation(simulation) 
    133         elif mode==CSTE_MODE_REMOTE_REPO: 
    134  
    135                 # prepare args 
    136                 # .. 
    137  
    138                 # execute 
    139                 repo.create_simulation(activity) 
    140  
    141                 # process return values 
    142                 # .. 
    143  
    144         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    145                 pass 
    146         else: 
    147                 raise Exception("ERR016 - incorrect mode") 
    148  
    149 def update_simulation_status(simulation): 
    150         if mode==CSTE_MODE_LOCAL_REPO: 
    151                 repo.update_simulation_status(simulation) 
    152         elif mode==CSTE_MODE_REMOTE_REPO: 
    153  
    154                 # prepare args 
    155                 # .. 
    156  
    157                 # execute 
    158                 repo.update_simulation_status(name) 
    159  
    160                 # process return values 
    161                 # .. 
    162  
    163         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    164                 pass 
    165         else: 
    166                 raise Exception("ERR017 - incorrect mode") 
    167  
    168 def retrieve_messages(simulation): 
    169         message=None 
    170  
    171         if mode==CSTE_MODE_LOCAL_REPO: 
    172                 message=repo.retrieve_messages(simulation) 
    173         elif mode==CSTE_MODE_REMOTE_REPO: 
    174  
    175                 # prepare args 
    176                 # .. 
    177  
    178                 # execute 
    179                 repo.retrieve_messages(name) 
    180  
    181                 # process return values 
    182                 # .. 
    183  
    184         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    185                 pass 
    186         else: 
    187                 raise Exception("ERR018 - incorrect mode") 
    188  
    189         return message 
    190  
    191 def delete_messages(simulation): 
    192         if mode==CSTE_MODE_LOCAL_REPO: 
    193                 repo.delete_messages(name) 
    194         elif mode==CSTE_MODE_REMOTE_REPO: 
    195  
    196                 # prepare args 
    197                 # .. 
    198  
    199                 # execute 
    200                 repo.delete_messages(name) 
    201  
    202                 # process return values 
    203                 # .. 
    204  
    205         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    206                 pass 
    207         else: 
    208                 raise Exception("ERR019 - incorrect mode") 
    209  
    210 def create_message(message): 
    211         if mode==CSTE_MODE_LOCAL_REPO: 
    212                 repo.create_message(message) 
    213         elif mode==CSTE_MODE_REMOTE_REPO: 
    214  
    215                 # prepare args 
    216                 # .. 
    217  
    218                 # execute 
    219                 repo.create_message() 
    220  
    221                 # process return values 
    222                 # .. 
    223  
    224         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    225                 pass 
    226         else: 
    227                 raise Exception("ERR020 - incorrect mode") 
    228  
    229 def retrieve_last_message(simulation): 
    230         message=None 
    231  
    232         if mode==CSTE_MODE_LOCAL_REPO: 
    233                 message=repo.retrieve_last_message(simulation) 
    234         elif mode==CSTE_MODE_REMOTE_REPO: 
    235  
    236                 # prepare args 
    237                 # .. 
    238  
    239                 # execute 
    240                 repo.retrieve_last_message(simulation) 
    241  
    242                 # process return values 
    243                 # .. 
    244  
    245         elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
    246                 pass 
    247         else: 
    248                 raise Exception("ERR021 - incorrect mode") 
    249  
    250         return message 
    251  
    252  
    253 # --- higher level methods --- # 
    254  
    255 def get_running_simulations(): 
    256         running_simulation=[] 
    257  
    258         for s in retrieve_simulation(): 
    259                 if s.status=="running": 
    260                         running_simulation.append(s) 
    261                          
    262         return running_simulation 
    263  
    264  
    265 # --- module init --- # 
     33# --- module static initialization --- # 
    26634 
    26735CSTE_MODE_LOCAL_REPO="local_repo" 
     
    28351 
    28452 
    285 # note that by putting init() call here, it will only be called when first imported by the first import statement 
    286 init() 
    287  
     53 
     54 
     55 
     56 
     57# -- methods -- # 
     58 
     59def init(): 
     60        if mode==CSTE_MODE_LOCAL_REPO: 
     61                repo.connect() 
     62        elif mode==CSTE_MODE_REMOTE_REPO: 
     63                _CONNECTION = "postgresql://postgres:Silence107!@localhost:5432/prodiguer" 
     64                prodiguer_shared.connect(_CONNECTION) 
     65        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     66                pass 
     67        else: 
     68                raise Exception("ERR004 - incorrect mode") 
     69 
     70def free(): 
     71        if mode==CSTE_MODE_LOCAL_REPO: 
     72                repo.free() 
     73        elif mode==CSTE_MODE_REMOTE_REPO: 
     74 
     75                #prodiguer_shared.close() 
     76                pass 
     77        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     78                pass 
     79        else: 
     80                raise Exception("ERR009 - incorrect mode") 
     81 
     82def populate_tables_with_sample(): 
     83        repo.populate_tables_with_sample() 
     84 
     85def retrieve_simulations(): 
     86        return repo.retrieve_simulations() 
     87 
     88def test(): 
     89        repo.create_message("test2", 2, "bla2") 
     90        commit() 
     91 
     92        repo.update_simulation_status('1pctCO22', 'ERROR') 
     93        commit() 
     94 
     95        repo.create_message("test3", 3, "bla3") 
     96        rollback() 
     97 
     98def commit(): 
     99        if mode==CSTE_MODE_LOCAL_REPO: 
     100                repo.commit() 
     101        elif mode==CSTE_MODE_REMOTE_REPO: 
     102                elixir.session.commit() 
     103        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     104                pass 
     105        else: 
     106                raise Exception("ERR002 - incorrect mode") 
     107 
     108def rollback(): 
     109        if mode==CSTE_MODE_LOCAL_REPO: 
     110                repo.rollback() 
     111        elif mode==CSTE_MODE_REMOTE_REPO: 
     112                elixir.session.rollback() 
     113        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     114                pass 
     115        else: 
     116                raise Exception("ERR003 - incorrect mode") 
     117 
     118def retrieve_simulation(name): 
     119        simulation=None 
     120 
     121        if mode==CSTE_MODE_LOCAL_REPO: 
     122                simulation=repo.retrieve_simulation(name) 
     123        elif mode==CSTE_MODE_REMOTE_REPO: 
     124 
     125                # prepare args 
     126                # .. 
     127 
     128                # execute 
     129                s=repo.retrieve_simulation(name) 
     130 
     131                # process return values 
     132                simulation=smon.types.Simulation(exec_start_date=s.ExecutionStartDate,exec_end_date=s.ExecutionEndDate,status=s.ExecutionState) # ExecutionState example: EXECUTION_STATE_RUNNING, EXECUTION_STATE_SET.. 
     133 
     134        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     135                pass 
     136        else: 
     137                raise Exception("ERR014 - incorrect mode") 
     138 
     139        return simulation 
     140 
     141def delete_simulation(name): 
     142        if mode==CSTE_MODE_LOCAL_REPO: 
     143                repo.delete_simulation(name) 
     144        elif mode==CSTE_MODE_REMOTE_REPO: 
     145 
     146                # prepare args 
     147                # .. 
     148 
     149                # execute 
     150                repo.delete_simulation(name) 
     151 
     152                # process return values 
     153                # .. 
     154 
     155        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     156                pass 
     157        else: 
     158                raise Exception("ERR015 - incorrect mode") 
     159 
     160def create_simulation(simulation): 
     161        if mode==CSTE_MODE_LOCAL_REPO: 
     162                repo.create_simulation(simulation) 
     163        elif mode==CSTE_MODE_REMOTE_REPO: 
     164 
     165                # prepare args 
     166                # .. 
     167 
     168                # execute 
     169                repo.create_simulation(activity) 
     170 
     171                # process return values 
     172                # .. 
     173 
     174        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     175                pass 
     176        else: 
     177                raise Exception("ERR016 - incorrect mode") 
     178 
     179def update_simulation_status(simulation): 
     180        if mode==CSTE_MODE_LOCAL_REPO: 
     181                repo.update_simulation_status(simulation) 
     182        elif mode==CSTE_MODE_REMOTE_REPO: 
     183 
     184                # prepare args 
     185                # .. 
     186 
     187                # execute 
     188                repo.update_simulation_status(name) 
     189 
     190                # process return values 
     191                # .. 
     192 
     193        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     194                pass 
     195        else: 
     196                raise Exception("ERR017 - incorrect mode") 
     197 
     198def retrieve_messages(simulation): 
     199        message=None 
     200 
     201        if mode==CSTE_MODE_LOCAL_REPO: 
     202                message=repo.retrieve_messages(simulation) 
     203        elif mode==CSTE_MODE_REMOTE_REPO: 
     204 
     205                # prepare args 
     206                # .. 
     207 
     208                # execute 
     209                repo.retrieve_messages(name) 
     210 
     211                # process return values 
     212                # .. 
     213 
     214        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     215                pass 
     216        else: 
     217                raise Exception("ERR018 - incorrect mode") 
     218 
     219        return message 
     220 
     221def delete_messages(simulation): 
     222        if mode==CSTE_MODE_LOCAL_REPO: 
     223                repo.delete_messages(name) 
     224        elif mode==CSTE_MODE_REMOTE_REPO: 
     225 
     226                # prepare args 
     227                # .. 
     228 
     229                # execute 
     230                repo.delete_messages(name) 
     231 
     232                # process return values 
     233                # .. 
     234 
     235        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     236                pass 
     237        else: 
     238                raise Exception("ERR019 - incorrect mode") 
     239 
     240def create_message(message): 
     241        if mode==CSTE_MODE_LOCAL_REPO: 
     242                repo.create_message(message) 
     243        elif mode==CSTE_MODE_REMOTE_REPO: 
     244 
     245                # prepare args 
     246                # .. 
     247 
     248                # execute 
     249                repo.create_message() 
     250 
     251                # process return values 
     252                # .. 
     253 
     254        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     255                pass 
     256        else: 
     257                raise Exception("ERR020 - incorrect mode") 
     258 
     259def retrieve_last_message(simulation): 
     260        message=None 
     261 
     262        if mode==CSTE_MODE_LOCAL_REPO: 
     263                message=repo.retrieve_last_message(simulation) 
     264        elif mode==CSTE_MODE_REMOTE_REPO: 
     265 
     266                # prepare args 
     267                # .. 
     268 
     269                # execute 
     270                repo.retrieve_last_message(simulation) 
     271 
     272                # process return values 
     273                # .. 
     274 
     275        elif mode==CSTE_MODE_REMOTE_REPO_STUB: 
     276                pass 
     277        else: 
     278                raise Exception("ERR021 - incorrect mode") 
     279 
     280        return message 
     281 
     282 
     283# --- higher level methods --- # 
     284 
     285def get_running_simulations(): 
     286        running_simulation=[] 
     287 
     288        for s in retrieve_simulation(): 
     289                if s.status=="running": 
     290                        running_simulation.append(s) 
     291                         
     292        return running_simulation 
     293 
     294 
Note: See TracChangeset for help on using the changeset viewer.