Mercurial > bottledash
annotate dash.py @ 32:a957a56bb694
release zur abgabe
author | sean |
---|---|
date | Sun, 23 Aug 2015 21:52:33 +0200 |
parents | 140c9023df7a |
children |
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(): |
25 | 82 logger.info("No config-file found. create new config file") |
8 | 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] | |
32 | 96 # show_top_bar=False |
29 | 97 # rows=3 |
7 | 98 # -------------------- |
99 # options: | |
100 # show_top_bar (True / False) [optional] | |
101 # If True, the dashboard will show the current date and the time on the top | |
102 # if False, the dashboard will not show a top-bar | |
29 | 103 # default: False |
7 | 104 # hint: the top-bar has got a height of 5%! |
29 | 105 # |
106 # rows (integer-number) [recommended] | |
107 # the number or rows for the tiles. Not every number makes sence since | |
108 # you usually cannot scroll on the screen. This only works on the default | |
109 # tile template - it is always possivle to write an own template! | |
110 # example: | |
111 # If there are 4 tiles in 2 rows the reult would be the following: | |
112 # ▢▢ | |
113 # ▢▢ | |
114 # | |
115 # if there are 3 tiles in 3 rows the result would be the following | |
116 # â–¢ | |
117 # â–¢ | |
118 # â–¢ | |
119 # | |
120 # if there re 12 tiles in 3 rows the reult would be the following | |
121 # ▢▢▢▢ | |
122 # ▢▢▢▢ | |
123 # ▢▢▢▢ | |
124 # | |
125 | |
7 | 126 |
127 [settings] | |
14 | 128 show_top_bar=False |
29 | 129 rows=3 |
7 | 130 |
131 # Tiles: | |
132 # example: | |
133 # -------------------- | |
134 # [tile1] | |
135 # type=mon | |
136 # source=192.168.0.2 | |
137 # status=up | |
138 # -------------------- | |
139 # options: | |
29 | 140 # type (mon / web_view) [required] |
7 | 141 # tells the program what kind of tile you need. |
142 # a "mon" tile can be used for IT infrastructure monitoring purposes | |
29 | 143 # a "web_view" tile can be used to display a chart |
7 | 144 # default: - |
145 # | |
146 # source (<IP> or <FQDN>) [required for mon-types] | |
147 # ONLY FOR MON-Type tiles! | |
148 # tells the tile which resource to watch | |
149 # ONLY FOR DEBUGGING PURPOSE - WILL BE REMOVED LATER | |
150 # simulates up and down events for mon-type-tiles | |
14 | 151 # |
152 # div_name: (identifier) [required for d3js-types] | |
153 # | |
154 # script: (name of a script without extension) [required for d3js-types0] | |
155 # | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
156 [tile1] |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
157 type=mon |
29 | 158 source=red2-Bottle-App |
28 | 159 status=wait |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
160 |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
161 [tile2] |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
162 type=mon |
29 | 163 source=Intevation_Homepage |
28 | 164 status=wait |
7 | 165 |
166 [tile3] | |
167 type=mon | |
29 | 168 source=mpuls-jmd-demo |
28 | 169 status=wait |
7 | 170 |
171 [tile4] | |
29 | 172 type=mon |
173 source=aise-icn | |
174 status=wait | |
175 | |
176 [tile5] | |
177 type=mon | |
178 source=mpuls-s_bew_prev | |
179 status=wait | |
180 | |
181 [tile6] | |
25 | 182 type=web_view |
14 | 183 div_name=techintern |
184 script=display_issues_techintern | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
185 """) |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
186 file.close() |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
187 |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
188 ##Bottle |
0 | 189 @route('/') |
15 | 190 @view('bottledash_view') |
0 | 191 def call_dashboard(): |
7 | 192 return dict(tiles=tiles, settings=settings) |
0 | 193 |
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
|
194 #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
|
195 @post('/updown') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
196 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
|
197 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
|
198 |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
199 service = request.forms.get('service') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
200 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 except KeyError: |
21 | 206 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
|
207 |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
208 |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
209 logger.info('------- new alert --------') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
210 logger.info('Service : ' + str(service)) |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
211 logger.info('Status : ' + str(status)) |
24 | 212 # log_tile_status() |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
213 |
25 | 214 return ":: " + str(service) + " is " + str(status) + " !" |
0 | 215 |
21 | 216 |
217 @post('/ask-systemstate') | |
218 def get_systemstate(): | |
219 global tiles | |
220 service = request.forms.get('service') | |
221 | |
25 | 222 # print("service: " + service) |
21 | 223 |
224 try: | |
225 for tile in tiles: | |
226 if tile["source"] == str(service): | |
227 # return "service : " + str(service) + " is " + str(tile["status"]) | |
228 return tile["status"] | |
229 except KeyError: | |
230 return "cannot find the service: " + str(service) | |
231 | |
232 return "did not found anything in my list for : " + service | |
233 | |
234 | |
235 @route('/static/<filename>') | |
236 def server_static(filename): | |
237 return static_file(filename, root=PATH + '/static/') | |
238 | |
0 | 239 @error(404) |
240 def error404(error): | |
241 return 'Nothing here, sorry <br /> 404' | |
242 | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
243 read_config() |
7 | 244 # print(tiles) |
25 | 245 run(host='localhost', port=8080, debug=False, quiet=True) |