Mercurial > bottledash
annotate dash.py @ 25:05e5441c5160
charts will now be displayed as they should!
author | sean |
---|---|
date | Fri, 14 Aug 2015 00:31:48 +0200 |
parents | 7d431b779512 |
children | af280057d5fa |
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] | |
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 |
25 | 107 rows=2 |
7 | 108 |
109 # Tiles: | |
110 # example: | |
111 # -------------------- | |
112 # [tile1] | |
113 # type=mon | |
114 # source=192.168.0.2 | |
115 # status=up | |
116 # -------------------- | |
117 # options: | |
118 # type (mon / d3js) [required] | |
119 # tells the program what kind of tile you need. | |
120 # a "mon" tile can be used for IT infrastructure monitoring purposes | |
121 # a d3js tile can be used to display a chart | |
122 # default: - | |
123 # | |
124 # source (<IP> or <FQDN>) [required for mon-types] | |
125 # ONLY FOR MON-Type tiles! | |
126 # tells the tile which resource to watch | |
127 # ONLY FOR DEBUGGING PURPOSE - WILL BE REMOVED LATER | |
128 # simulates up and down events for mon-type-tiles | |
14 | 129 # |
130 # div_name: (identifier) [required for d3js-types] | |
131 # | |
132 # script: (name of a script without extension) [required for d3js-types0] | |
133 # | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
134 [tile1] |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
135 type=mon |
25 | 136 source=red2.rgb.intevation.de |
137 status="dynamic" | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
138 |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
139 [tile2] |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
140 type=mon |
7 | 141 source=192.168.2.3 |
25 | 142 status="dynamic" |
7 | 143 |
144 [tile3] | |
145 type=mon | |
146 source=192.168.4.3 | |
25 | 147 status="dynamic" |
7 | 148 |
149 [tile4] | |
25 | 150 type=web_view |
14 | 151 div_name=techintern |
152 script=display_issues_techintern | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
153 """) |
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
154 file.close() |
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 ##Bottle |
0 | 157 @route('/') |
15 | 158 @view('bottledash_view') |
0 | 159 def call_dashboard(): |
7 | 160 return dict(tiles=tiles, settings=settings) |
0 | 161 |
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
|
162 #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
|
163 @post('/updown') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
164 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
|
165 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
|
166 |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
167 service = request.forms.get('service') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 except KeyError: |
21 | 174 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
|
175 |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
176 |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
177 logger.info('------- new alert --------') |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
178 logger.info('Service : ' + str(service)) |
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
179 logger.info('Status : ' + str(status)) |
24 | 180 # log_tile_status() |
19
be6a1aaec717
added the post listener, write logs if someone sends a post
sean
parents:
15
diff
changeset
|
181 |
25 | 182 return ":: " + str(service) + " is " + str(status) + " !" |
0 | 183 |
21 | 184 |
185 @post('/ask-systemstate') | |
186 def get_systemstate(): | |
187 global tiles | |
188 service = request.forms.get('service') | |
189 | |
25 | 190 # print("service: " + service) |
21 | 191 |
192 try: | |
193 for tile in tiles: | |
194 if tile["source"] == str(service): | |
195 # return "service : " + str(service) + " is " + str(tile["status"]) | |
196 return tile["status"] | |
197 except KeyError: | |
198 return "cannot find the service: " + str(service) | |
199 | |
200 return "did not found anything in my list for : " + service | |
201 | |
202 | |
203 @route('/static/<filename>') | |
204 def server_static(filename): | |
205 return static_file(filename, root=PATH + '/static/') | |
206 | |
0 | 207 @error(404) |
208 def error404(error): | |
209 return 'Nothing here, sorry <br /> 404' | |
210 | |
3
3f5bcad45756
pars a .conf | dynamic adding of divs | dynamiv VP scaling
sean
parents:
0
diff
changeset
|
211 read_config() |
7 | 212 # print(tiles) |
25 | 213 run(host='localhost', port=8080, debug=False, quiet=True) |