Mercurial > bottledash
annotate dash.py @ 21:f730dd0bcf85
dynamic checking of the tile-status
author | sean |
---|---|
date | Thu, 13 Aug 2015 12:17:46 +0200 |
parents | 1a13a4ecf931 |
children | 7d431b779512 |
rev | line source |
---|---|
4
3e66e2f92770
added LICENSE and license headers in the sourcecode. Added readme
sean
parents:
3
diff
changeset
|
1 """ |
3e66e2f92770
added LICENSE and license headers in the sourcecode. Added readme
sean
parents:
3
diff
changeset
|
2 This software is part of "Bottledash" |
3e66e2f92770
added LICENSE and license headers in the sourcecode. Added readme
sean
parents:
3
diff
changeset
|
3 author: sean engelhardt > sean.engelhardt@intevation.de |
3e66e2f92770
added LICENSE and license headers in the sourcecode. Added readme
sean
parents:
3
diff
changeset
|
4 license: GNU >= V2. See LICENSE for details |
3e66e2f92770
added LICENSE and license headers in the sourcecode. Added readme
sean
parents:
3
diff
changeset
|
5 """ |
3e66e2f92770
added LICENSE and license headers in the sourcecode. Added readme
sean
parents:
3
diff
changeset
|
6 |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
7 from bottle import get, post, request, view, response, route |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
8 from bottle import template, run, static_file, error |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
9 import os.path, sys, configparser, functools, bottle |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
10 import logging |
0 | 11 |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
12 CONFIG_FILE = "dash.conf" |
8 | 13 PATH = os.path.abspath(os.path.dirname(sys.argv[0])) |
14 CONFIG_PATH = PATH + "/" + CONFIG_FILE | |
15 | |
9 | 16 # Create a new list with absolute paths |
17 MY_TEMPLATE_PATH = [ | |
10 | 18 os.path.abspath(os.path.join(os.path.dirname(__file__), './views')), |
9 | 19 ] |
20 | |
21 # Patch @view() so it uses the customized path list instead of the global one | |
22 view = functools.partial(bottle.view, template_lookup=MY_TEMPLATE_PATH) | |
23 | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
24 tiles = [] |
7 | 25 settings = {} |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
26 |
7 | 27 default_settings = configparser.ConfigParser() |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
28 default_settings['settings'] = {'show_top_bar': False, 'rows': 2} |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
29 |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
30 #logging: |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
31 |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
32 logger = logging.getLogger('myapp') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
33 hdlr = logging.FileHandler(PATH + '/bottledash.log') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
34 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
35 hdlr.setFormatter(formatter) |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
36 logger.addHandler(hdlr) |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
37 logger.setLevel(logging.INFO) |
7 | 38 |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
39 ### debug function: prints the current status of the "tiles" in |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
40 ### the log-file |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
41 def log_tile_status(): |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
42 global tiles |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
43 |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
44 try: |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
45 for tile in tiles: |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
46 logger.info("found tile : " + str(tile)) |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
47 for option in tiles[0]: |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
48 logger.info(str(option) + ' : ' + str(tile[option])) |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
49 except KeyError: |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
50 pass |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
51 |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
52 ###read the config file. usually "dash.conf" |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
53 def read_config(): |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
54 global settings |
8 | 55 # print(PATH) |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
56 |
8 | 57 if os.path.isfile(CONFIG_PATH) == False: |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
58 write_default_config() |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
59 |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
60 # print("read existing config file...") |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
61 config = configparser.ConfigParser() |
8 | 62 config.read(CONFIG_PATH) |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
63 |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
64 for section in config.items(): |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
65 if "tile" in section[0]: |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
66 tiles.append(section[1]) |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
67 |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
68 ###log read tiles |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
69 logger.info("------- read tiles --------") |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
70 log_tile_status() |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
71 |
7 | 72 try: |
73 if config["settings"]: | |
74 settings = config["settings"] | |
75 else: | |
76 settings = default_settings["settings"] | |
77 except KeyError: | |
78 settings = default_settings["settings"] | |
79 | |
80 ###write the default condfig file if there is none | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
81 def write_default_config(): |
8 | 82 print("there is no Config file! Creating one...") |
83 file = open(CONFIG_PATH, "w") | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
84 file.write(""" |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
85 ### bottledash default configuration |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
86 ### created by sean engelhardt >sean.engelhardt@intevation.de |
7 | 87 ### license: GNU GPL >= v2. See LICENSE for details |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
88 ### |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
89 ### Usage: |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
90 ### define the tiles for the dashboard in sections |
7 | 91 |
92 # Settings: | |
93 # example: | |
94 # -------------------- | |
95 # [settings] | |
96 # show_top_bar=True | |
97 # -------------------- | |
98 # options: | |
99 # show_top_bar (True / False) [optional] | |
100 # If True, the dashboard will show the current date and the time on the top | |
101 # if False, the dashboard will not show a top-bar | |
102 # default: True | |
103 # hint: the top-bar has got a height of 5%! | |
104 | |
105 [settings] | |
14 | 106 show_top_bar=False |
7 | 107 |
108 # Tiles: | |
109 # example: | |
110 # -------------------- | |
111 # [tile1] | |
112 # type=mon | |
113 # source=192.168.0.2 | |
114 # status=up | |
115 # -------------------- | |
116 # options: | |
117 # type (mon / d3js) [required] | |
118 # tells the program what kind of tile you need. | |
119 # a "mon" tile can be used for IT infrastructure monitoring purposes | |
120 # a d3js tile can be used to display a chart | |
121 # default: - | |
122 # | |
123 # source (<IP> or <FQDN>) [required for mon-types] | |
124 # ONLY FOR MON-Type tiles! | |
125 # tells the tile which resource to watch | |
126 # default: none | |
127 # | |
128 # status: (up / down) [required] | |
129 # ONLY FOR DEBUGGING PURPOSE - WILL BE REMOVED LATER | |
130 # simulates up and down events for mon-type-tiles | |
14 | 131 # |
132 # div_name: (identifier) [required for d3js-types] | |
133 # | |
134 # script: (name of a script without extension) [required for d3js-types0] | |
135 # | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
136 [tile1] |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
137 type=mon |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
138 source=192.168.0.2 |
14 | 139 status=down |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
140 |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
141 [tile2] |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
142 type=mon |
7 | 143 source=192.168.2.3 |
14 | 144 status=up |
7 | 145 |
146 [tile3] | |
147 type=mon | |
148 source=192.168.4.3 | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
149 status=up |
7 | 150 |
151 [tile4] | |
14 | 152 type=d3js |
153 div_name=techintern | |
154 script=display_issues_techintern | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
155 """) |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
156 file.close() |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
157 |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
158 ##Bottle |
0 | 159 @route('/') |
15 | 160 @view('bottledash_view') |
0 | 161 def call_dashboard(): |
7 | 162 return dict(tiles=tiles, settings=settings) |
0 | 163 |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
164 #wait for post-request which shall inform the system about running services |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
165 @post('/updown') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
166 def updown(): |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
167 global tiles |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
168 |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
169 service = request.forms.get('service') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
170 status = request.forms.get('status') |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
171 try: |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
172 for tile in tiles: |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
173 if tile["source"] == str(service): |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
174 tile["status"] = str(status) |
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
175 except KeyError: |
21 | 176 logger.info("this tile got no source : " + str(tile)) |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
177 |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
178 |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
179 logger.info('------- new alert --------') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
180 logger.info('Service : ' + str(service)) |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
181 logger.info('Status : ' + str(status)) |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
182 log_tile_status() |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
183 |
20
1a13a4ecf931
can not recive post-requests wich should deliver if a server is up or down. Updated after a refresh of the webpage, wich will automatically done after 5 seconds
sean
parents:
19
diff
changeset
|
184 return "thanks for informing bottledash! :: " + str(service) + " is " + str(status) + " !" |
0 | 185 |
21 | 186 |
187 @post('/ask-systemstate') | |
188 def get_systemstate(): | |
189 global tiles | |
190 service = request.forms.get('service') | |
191 | |
192 print("service: " + service) | |
193 | |
194 try: | |
195 for tile in tiles: | |
196 if tile["source"] == str(service): | |
197 # return "service : " + str(service) + " is " + str(tile["status"]) | |
198 return tile["status"] | |
199 except KeyError: | |
200 return "cannot find the service: " + str(service) | |
201 | |
202 return "did not found anything in my list for : " + service | |
203 | |
204 | |
205 @route('/static/<filename>') | |
206 def server_static(filename): | |
207 return static_file(filename, root=PATH + '/static/') | |
208 | |
0 | 209 @error(404) |
210 def error404(error): | |
211 return 'Nothing here, sorry <br /> 404' | |
212 | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
213 read_config() |
7 | 214 # print(tiles) |
9 | 215 run(host='localhost', port=8080, debug=True) |