|
1 | | -#!/usr/bin/python |
2 | | -''' |
3 | | -Created on Mar 19, 2012 |
4 | | -
|
5 | | -A python script to ensure that the cf-update-ioc is working correctly. |
6 | | -The script requires the setup of two files |
7 | | -e.g. |
8 | | ->cat nagios01.host01.dbl |
9 | | ->test:cf-update-daemon{test} |
10 | | -
|
11 | | ->cat nagios02.host02.dbl |
12 | | ->test:cf-update-daemon{test} |
13 | | -
|
14 | | -The script will touch each file, with a short delay and will check that |
15 | | -Channelfinder has been appropriately updated. |
16 | | -
|
17 | | -python cf-monitor-test /complete/path/to/daemon/dir -i initialFile -f finalFile |
18 | | -
|
19 | | -@author: shroffk |
20 | | -''' |
21 | | -import sys |
22 | | -import os |
23 | | -import re |
24 | | -from optparse import OptionParser |
25 | | -from time import sleep |
26 | | - |
27 | | -from channelfinder import ChannelFinderClient |
28 | | - |
29 | | -SEVR = {0:'OK ', |
30 | | - 1:'Minor ', |
31 | | - 2:'Major '} |
32 | | - |
33 | | - |
34 | | -def main(): |
35 | | - requiredOpts = ['initial-file', 'final-file'] |
36 | | - usage = "usage: %prog -i initial-file -f final-file directory " |
37 | | - parser = OptionParser(usage=usage) |
38 | | - parser.add_option('-i', '--initial-file', \ |
39 | | - action='store', type='string', dest='initialFile', \ |
40 | | - help='the initial-file') |
41 | | - parser.add_option('-f', '--final-file', \ |
42 | | - action='store', type='string', dest='finalFile', \ |
43 | | - help='the --final-file') |
44 | | - opts, args = parser.parse_args() |
45 | | - if args == None or len(args) == 0 : |
46 | | - parser.error('Please specify a directory') |
47 | | - if not opts.initialFile: |
48 | | - parser.error('Please specify a initial test files') |
49 | | - if not opts.finalFile: |
50 | | - parser.error('Please specify a final test files') |
51 | | - mainRun(opts, args) |
52 | | - |
53 | | - |
54 | | -def mainRun(opts, args): |
55 | | - for directory in args: |
56 | | - initialFile = os.path.normpath(directory + '/' + opts.initialFile) |
57 | | - iHostName, iIocName = getArgsFromFilename(initialFile) |
58 | | - finalFile = os.path.normpath(directory + '/' + opts.finalFile) |
59 | | - fHostName, fIocName = getArgsFromFilename(finalFile) |
60 | | - if getPVNames(initialFile) != getPVNames(finalFile): |
61 | | - sys.exit(1) |
62 | | - pvNames = getPVNames(initialFile) |
63 | | - if len(pvNames) == 0: |
64 | | - sys.exit(1) |
65 | | - ''' |
66 | | - Touch the initial file and check channelfinder |
67 | | - ''' |
68 | | - touch(initialFile) |
69 | | - sleep(2) |
70 | | - check(pvNames, iHostName, iIocName) |
71 | | - ''' |
72 | | - Touch the final file and check channelfinder |
73 | | - ''' |
74 | | - touch(finalFile) |
75 | | - sleep(2) |
76 | | - check(pvNames, fHostName, fIocName) |
77 | | - sys.exit |
78 | | - |
79 | | - |
80 | | -def check(pvNames, hostName, iocName): |
81 | | - try: |
82 | | - client = ChannelFinderClient() |
83 | | - except: |
84 | | - raise RuntimeError('Unable to create a valid webResourceClient') |
85 | | - channels = client.find(property=[('hostName', hostName), ('iocName', iocName)]) |
86 | | - if channels and len(pvNames) == len(channels): |
87 | | - for channel in channels: |
88 | | - if channel.Name not in pvNames: |
89 | | - sys.exit(2) |
90 | | - else: |
91 | | - sys.exit(2) |
92 | | - |
93 | | - |
94 | | -def touch(fname, times=None): |
95 | | - with open(fname, 'a'): |
96 | | - os.utime(fname, times) |
97 | | - |
98 | | - |
99 | | -def getArgsFromFilename(completeFilePath): |
100 | | - fileName = os.path.split(os.path.normpath(completeFilePath))[1] |
101 | | - pattern4Hostname = '(\S+?)\.\S+' |
102 | | - match = re.search(pattern4Hostname, fileName) |
103 | | - if match: |
104 | | - hostName = match.group(1) |
105 | | - else: |
106 | | - hostName = None |
107 | | - pattern4Iocname = '\S+?\.(\S+?)\.\S+' |
108 | | - match = re.search(pattern4Iocname, fileName) |
109 | | - if match: |
110 | | - iocName = match.group(1) |
111 | | - else: |
112 | | - iocName = None |
113 | | - return hostName, iocName |
114 | | - |
115 | | -def getPVNames(completeFilePath, pattern=None): |
116 | | - try: |
117 | | - f = open(completeFilePath) |
118 | | - pvNames = f.read().splitlines() |
119 | | - pvNames = map(lambda x: x.strip(), pvNames) |
120 | | - pvNames = filter(lambda x: len(x) > 0, pvNames) |
121 | | - if pattern: |
122 | | - pvNames = [ re.match(pattern, pvName).group() for pvName in pvNames if re.match(pattern, pvName) ] |
123 | | - return pvNames |
124 | | - except IOError: |
125 | | - return None |
126 | | - finally: |
127 | | - f.close() |
128 | | - |
129 | | -if __name__ == '__main__': |
130 | | - main() |
131 | | - pass |
| 1 | +#!/usr/bin/python |
| 2 | +''' |
| 3 | +Created on Mar 19, 2012 |
| 4 | +
|
| 5 | +A python script to ensure that the cf-update-ioc is working correctly. |
| 6 | +The script requires the setup of two files |
| 7 | +e.g. |
| 8 | +>cat nagios01.host01.dbl |
| 9 | +>test:cf-update-daemon{test} |
| 10 | +
|
| 11 | +>cat nagios02.host02.dbl |
| 12 | +>test:cf-update-daemon{test} |
| 13 | +
|
| 14 | +The script will touch each file, with a short delay and will check that |
| 15 | +Channelfinder has been appropriately updated. |
| 16 | +
|
| 17 | +python cf-monitor-test /complete/path/to/daemon/dir -i initialFile -f finalFile |
| 18 | +
|
| 19 | +@author: shroffk |
| 20 | +''' |
| 21 | +import sys |
| 22 | +import os |
| 23 | +import re |
| 24 | +from optparse import OptionParser |
| 25 | +from time import sleep |
| 26 | + |
| 27 | +from channelfinder import ChannelFinderClient |
| 28 | + |
| 29 | +SEVR = {0:'OK ', |
| 30 | + 1:'Minor ', |
| 31 | + 2:'Major '} |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + requiredOpts = ['initial-file', 'final-file'] |
| 36 | + usage = "usage: %prog -i initial-file -f final-file directory " |
| 37 | + parser = OptionParser(usage=usage) |
| 38 | + parser.add_option('-i', '--initial-file', \ |
| 39 | + action='store', type='string', dest='initialFile', \ |
| 40 | + help='the initial-file') |
| 41 | + parser.add_option('-f', '--final-file', \ |
| 42 | + action='store', type='string', dest='finalFile', \ |
| 43 | + help='the --final-file') |
| 44 | + opts, args = parser.parse_args() |
| 45 | + if args == None or len(args) == 0 : |
| 46 | + parser.error('Please specify a directory') |
| 47 | + if not opts.initialFile: |
| 48 | + parser.error('Please specify a initial test files') |
| 49 | + if not opts.finalFile: |
| 50 | + parser.error('Please specify a final test files') |
| 51 | + mainRun(opts, args) |
| 52 | + |
| 53 | + |
| 54 | +def mainRun(opts, args): |
| 55 | + for directory in args: |
| 56 | + initialFile = os.path.normpath(directory + '/' + opts.initialFile) |
| 57 | + iHostName, iIocName = getArgsFromFilename(initialFile) |
| 58 | + finalFile = os.path.normpath(directory + '/' + opts.finalFile) |
| 59 | + fHostName, fIocName = getArgsFromFilename(finalFile) |
| 60 | + if getPVNames(initialFile) != getPVNames(finalFile): |
| 61 | + sys.exit(1) |
| 62 | + pvNames = getPVNames(initialFile) |
| 63 | + if len(pvNames) == 0: |
| 64 | + sys.exit(1) |
| 65 | + ''' |
| 66 | + Touch the initial file and check channelfinder |
| 67 | + ''' |
| 68 | + touch(initialFile) |
| 69 | + sleep(2) |
| 70 | + check(pvNames, iHostName, iIocName) |
| 71 | + ''' |
| 72 | + Touch the final file and check channelfinder |
| 73 | + ''' |
| 74 | + touch(finalFile) |
| 75 | + sleep(2) |
| 76 | + check(pvNames, fHostName, fIocName) |
| 77 | + sys.exit |
| 78 | + |
| 79 | + |
| 80 | +def check(pvNames, hostName, iocName): |
| 81 | + try: |
| 82 | + client = ChannelFinderClient() |
| 83 | + except: |
| 84 | + raise RuntimeError('Unable to create a valid webResourceClient') |
| 85 | + channels = client.find(property=[('hostName', hostName), ('iocName', iocName)]) |
| 86 | + if channels and len(pvNames) == len(channels): |
| 87 | + for channel in channels: |
| 88 | + if channel.Name not in pvNames: |
| 89 | + sys.exit(2) |
| 90 | + else: |
| 91 | + sys.exit(2) |
| 92 | + |
| 93 | + |
| 94 | +def touch(fname, times=None): |
| 95 | + with open(fname, 'a'): |
| 96 | + os.utime(fname, times) |
| 97 | + |
| 98 | + |
| 99 | +def getArgsFromFilename(completeFilePath): |
| 100 | + fileName = os.path.split(os.path.normpath(completeFilePath))[1] |
| 101 | + pattern4Hostname = '(\S+?)\.\S+' |
| 102 | + match = re.search(pattern4Hostname, fileName) |
| 103 | + if match: |
| 104 | + hostName = match.group(1) |
| 105 | + else: |
| 106 | + hostName = None |
| 107 | + pattern4Iocname = '\S+?\.(\S+?)\.\S+' |
| 108 | + match = re.search(pattern4Iocname, fileName) |
| 109 | + if match: |
| 110 | + iocName = match.group(1) |
| 111 | + else: |
| 112 | + iocName = None |
| 113 | + return hostName, iocName |
| 114 | + |
| 115 | +def getPVNames(completeFilePath, pattern=None): |
| 116 | + try: |
| 117 | + f = open(completeFilePath) |
| 118 | + pvNames = f.read().splitlines() |
| 119 | + pvNames = map(lambda x: x.strip(), pvNames) |
| 120 | + pvNames = filter(lambda x: len(x) > 0, pvNames) |
| 121 | + if pattern: |
| 122 | + pvNames = [ re.match(pattern, pvName).group() for pvName in pvNames if re.match(pattern, pvName) ] |
| 123 | + return pvNames |
| 124 | + except IOError: |
| 125 | + return None |
| 126 | + finally: |
| 127 | + f.close() |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + main() |
| 131 | + pass |
0 commit comments