forked from codefitz/tplser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.tpl
353 lines (288 loc) · 11.6 KB
/
example.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// This pattern is in the public domain
tpl 1.0 module Wibble;
metadata
origin := "Example";
tree_path := 'Example', 'Wibble';
end metadata;
// We need the Apache Webserver from TKU to create the BAI herein
from ApacheBasedWebservers import ApacheBasedWebserver 1.2;
// Lookup product_version attribute for Appserver
table AppserverProductVersions 1.0
'0.5' -> 'Alpha';
'2.6' -> 'NG';
'2.7' -> 'NGi';
'3.0' -> 'Ultima';
default -> 'Unknown';
end table;
//
// Generate SoftwareInstance for WibbleDatabase
//
pattern WibbleDatabase 1.0
'''
Pattern to capture WibbleDatabase SI. It detects the process and obtains version
information by the 2 digits in the process name. The instance name is extracted
from the process name after the "_".
Supported Platforms:
Unix
To Do:
Implement Windows version
'''
metadata
publishers := 'Wibble Software Company Ltd';
categories := 'Database Server Software';
urls := 'http://www.wibble.com/';
end metadata;
overview
tags Training3, Wibble;
end overview;
constants
db_type := 'Wibble Database Server';
end constants;
triggers
on process := DiscoveredProcess where cmd matches unix_cmd 'wibbleDB\d+_.*';
end triggers;
body
host := model.host(process);
// Extract instance; eg SLAVE from a command like "wibbleDB35_SLAVE"
instance := regex.extract(process.cmd, regex 'wibbleDB\d+_(.*)', raw '\1');
// Extract version; eg 3.5 from a command like "wibbleDB35_SLAVE"
version := regex.extract(process.cmd, regex 'wibbleDB(\d)(\d+)', raw '\1.\2');
// Create or update datastore
model.SoftwareInstance(name := '%db_type% %version% instance "%instance%" on %host.name%',
type := db_type, instance := instance,
key := '%instance%/%db_type%/%host.key%',
version := version, product_version := version);
end body;
end pattern;
//
// Generate SoftwareInstance for WibbleAppserver
//
pattern WibbleAppserver 1.0
'''
Pattern to capture WibbleAppserver SI. It detects the process and obtains version
information by running "-v". The configuration file is extracted from the command
line "-c" option, and interrogated for various attributes.
Supported Platforms:
Unix
To Do:
Implement Windows version
'''
metadata
publishers := 'Wibble Software Company Ltd';
categories := 'Application Server Software';
urls := 'http://www.wibble.com/';
known_versions := '0.5', '2.6', '2.7', '3.0';
end metadata;
overview
tags Training3, Wibble;
end overview;
constants
appserver_type := 'Wibble Application Server';
db_type := 'Wibble Database Server';
end constants;
triggers
on process := DiscoveredProcess where cmd matches unix_cmd 'wibbleAppServ';
end triggers;
body
host := model.host(process);
cmdResult := discovery.runCommand(host, process.cmd + ' -v');
if not cmdResult then
log.warn('Failed to run command %process.cmd% on host %host.name%');
version := '';
build := '';
patch := '';
else
// Extract versions from command result like "Version 2.7.182 patch 69"
version := regex.extract(cmdResult.result, regex 'Version (\d+\.\d+)', raw '\1');
build := regex.extract(cmdResult.result, regex 'Version \d+\.\d+\.(\d+)', raw '\1');
patch := regex.extract(cmdResult.result, regex 'patch (\d+)', raw '\1');
end if;
// Extract config file from argument: file after a "-c"
cfgfilePath := regex.extract(process.args, regex '-c\s+(\S+)', raw '\1');
// Extract instance from command path (last directory before file): eg SLAVE in "/.../wibble/SLAVE/config.xml"
instance := regex.extract(cfgfilePath, regex '/(\w+)/[\w.]+$', raw '\1');
// Get config file
cfgfile := discovery.fileGet(host, cfgfilePath);
portstr := '';
edition := '';
if not cfgfile then
log.warn('Config file %cfgfilePath% not found');
else
// Extract edition
edition := xpath.evaluate(cfgfile.content, raw '/config/engine/@edition');
if not edition then
edition := '';
else
edition := text.lower(edition[0]);
end if;
// Extract port information and generate a space-string
// This example is contrived, as you could (and probably should) just attach a list to
// the host node, but it shows what can be done with a for loop.
ports := xpath.evaluate(cfgfile.content, raw '/config/listeners/listener//@port');
for port in ports do
if portstr = '' then
portstr := port;
else
portstr := '%portstr% %port%';
end if;
end for;
end if;
// Get application version (the webapp that is running in the appserver, not the version of
// the appserver itself) from startup file; qexpense.log in the same directory as the config file
// This version is put in custom attribute qe_version, so it can be used by the BAI pattern.
appfilePath := regex.extract(process.args, regex '-c\s+(\S+/)', raw '\1qexpense.log');
// Get startup log file
appfile := discovery.fileGet(host, appfilePath);
if not appfile then
log.warn('Appserver log file %appfilePath% not found');
qe_version := '';
else
qe_maj := regex.extract(appfile.content, regex 'Major Release (\d+)', raw '\1');
qe_min := regex.extract(appfile.content, regex 'Minor Release (\d+)', raw '\1');
qe_version := '%qe_maj%.%qe_min%';
end if;
// Create or update datastore
si := model.SoftwareInstance(name := '%appserver_type% %version% instance "%instance%" on %host.name%',
type := appserver_type, instance := instance,
key := '%instance%/%appserver_type%/%host.key%',
version := version, product_version := AppserverProductVersions[version],
patch := patch, build := build,
ports := portstr, edition := edition,
_tw_meta_data_attrs := ['ports'],
qe_version := qe_version);
// Create dependency to database SI
db_si := search(in host traverse Host:HostedSoftware:RunningSoftware:SoftwareInstance where
type = %db_type% and instance = %instance%);
if not db_si then
log.warn('No database SIs found to link appserver to');
else
// We expect to have only have one DB present, so could take the first,
// but might as well pass the whole list
model.rel.Dependency(Dependant := si, DependedUpon := db_si);
end if;
end body;
end pattern;
//
// Generate SoftwareInstance for Wibble Exporter
//
pattern WibbleExporter 1.0
'''
Pattern to capture Wibble Exporter SI.
Version information captured from the installed package
No instance information is captured
Supported Platforms:
Unix
To Do:
Implement Windows version
'''
metadata
publishers := 'Wibble Software Company Ltd';
urls := 'http://www.wibble.com/';
end metadata;
overview
tags Training3, Wibble;
end overview;
constants
exp_type := 'Wibble Exporter Server';
end constants;
triggers
on process := DiscoveredProcess where cmd matches unix_cmd 'wexportd';
end triggers;
body
host := model.host(process);
// Try to find version from installed package list
packages := model.findPackages(host, [regex '^wibble$']);
version := '';
revision := '';
if not packages then
log.warn('Could not get version of %exp_type% from package list');
else
version := packages[0].version;
revision := packages[0].revision;
end if;
// Create or update datastore
model.SoftwareInstance(name := '%exp_type% %version% on %host.name%',
type := exp_type,
version := version, product_version := version,
revision := revision);
end body;
end pattern;
//
// Generate BusinessApplicationInstance for QuickExpense
//
pattern QuickExpense 1.0
'''
Pattern to generate the QuickExpense Business Application
It is triggered when the Wibble Application Server is detected, but only created when
in addition, a suitable Web server is also found (Apache with version of at least 2.1).
Associated database servers that support the application servers are linked in.
Associated exporter servers are linked in.
'''
overview
tags Training3, QuickExpense;
requires ApacheBasedWebserver;
end overview;
constants
appserver_type := 'Wibble Application Server';
bai_type := 'QuickExpense Web Application';
exp_type := 'Wibble Exporter Server';
webserver_type := 'Apache Webserver'; // SI pattern from TKU
end constants;
triggers
on si := SoftwareInstance created, modified where type = appserver_type;
end triggers;
body
host := model.host(si);
// Assume that any instance of Apache on this host is also part of the application
// WARNING: string comparison of versions performed in search query
webserver_sis := search(in host traverse Host:HostedSoftware:RunningSoftware:SoftwareInstance where
type = %webserver_type% and product_version >= '2.1');
if not webserver_sis then
log.warn('No webserver SIs found, not creating BAI');
stop;
end if;
// Create or update datastore with the BAI now we know we have both an Appserver and Webserver
// WARNING: we assume the qe_version is the same, obtained from either appserver SI
bai := model.BusinessApplicationInstance(name := '%bai_type% %si.qe_version% on %host.name%',
type := bai_type,
key := '%bai_type%/%host.key%',
version := si.qe_version, product_version := si.qe_version);
// Create a software containment relationship to appserver and webserver SIs (required for the BAI to exist)
model.addContainment(bai, si);
model.addContainment(bai, webserver_sis);
// Create relationships to database SIs (the only dependency from the Appserver SI should be the DB SI)
db_sis := search(in si traverse Dependant:Dependency:DependedUpon:SoftwareInstance);
if not db_sis then
log.warn('No database SIs found to link BAI to');
else
// We should only have one database present, but more that one would be handled too
model.addContainment(bai, db_sis);
end if;
// Create relationships to exporter SIs
exp_sis := search(in host traverse Host:HostedSoftware:RunningSoftware:SoftwareInstance where
type = %exp_type%);
if not exp_sis then
log.warn('No exporter SIs found to link BAI to');
else
// We may find more than one exporter
model.addContainment(bai, exp_sis);
end if;
end body;
end pattern;
//
// Simple Identifier Matchers
//
// Simple identities matching cmd only
identify Servers_Wibble 1.0
tags Training3, Wibble;
DiscoveredProcess cmd -> simple_identity;
unix_cmd 'wibbleAppServ' -> 'Wibble Application Server';
unix_cmd 'wibbleDB\d+_.*' -> 'Wibble Database Server';
unix_cmd 'wexportd' -> 'Wibble Exporter Server';
end identify;
// Simple identities matching cmd and args (wdog is too generic a name on its own)
identify Servers_Wibble_Watchdog 1.0
tags Training3, Wibble;
DiscoveredProcess cmd, args -> simple_identity;
unix_cmd 'wdog', regex 'wibbleAppServ' -> 'Wibble Application Server Watchdog Service';
end identify;