/
HostsList.java
605 lines (526 loc) · 19.8 KB
/
HostsList.java
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package jrds;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import jrds.PropertiesManager.TimerInfo;
import jrds.configuration.ConfigObjectFactory;
import jrds.factories.ArgFactory;
import jrds.factories.ProbeMeta;
import jrds.graphe.Sum;
import jrds.starter.HostStarter;
import jrds.starter.Listener;
import jrds.starter.Starter;
import jrds.starter.StarterNode;
import jrds.webapp.ACL;
import jrds.webapp.DiscoverAgent;
import jrds.webapp.RolesACL;
import org.apache.log4j.Level;
/**
* The central repository of all informations : hosts, graph, and everything
* else
*
* @author Fabrice Bacchella
*/
public class HostsList extends StarterNode {
static final private AtomicInteger generation = new AtomicInteger(0);
private final int thisgeneration = generation.incrementAndGet();
private final Set<HostInfo> hostList = new HashSet<HostInfo>();
private final Set<Starter> topStarters = new HashSet<Starter>();
private final Map<String, jrds.starter.Timer> timers = new HashMap<String, jrds.starter.Timer>();
private final Map<Integer, GraphNode> graphMap = new HashMap<Integer, GraphNode>();
private final Map<Integer, Probe<?, ?>> probeMap = new HashMap<Integer, Probe<?, ?>>();
private final Map<String, GraphTree> treeMap = new LinkedHashMap<String, GraphTree>(3);
private final Map<String, Filter> filters = new TreeMap<String, Filter>(String.CASE_INSENSITIVE_ORDER);
private Map<String, Tab> tabs = new LinkedHashMap<String, Tab>();
private String firstTab = null;
private Renderer renderer = null;
private Timer collectTimer;
// The list of roles known to jrds
private Set<String> roles = new HashSet<String>();
private Set<String> defaultRoles = Collections.emptySet();
// A global flag that tells globally that this HostsList can be used
volatile private boolean started = false;
private Set<Class<? extends DiscoverAgent>> daList = new HashSet<Class<? extends DiscoverAgent>>();
/**
*
*/
public HostsList() {
super();
init();
}
/**
*
*/
public HostsList(PropertiesManager pm) {
super();
init();
configure(pm);
}
private void init() {
filters.put(Filter.EVERYTHING.getName(), Filter.EVERYTHING);
addTree(Filter.ALLHOSTS.getName(), GraphTree.HOSTROOT);
filters.put(Filter.ALLHOSTS.getName(), Filter.ALLHOSTS);
GraphTree viewtree = addTree(Filter.ALLVIEWS.getName(), GraphTree.VIEWROOT);
viewtree.addPath("Services");
filters.put(Filter.ALLVIEWS.getName(), Filter.ALLVIEWS);
treeMap.put(Filter.ALLSERVICES.getName(), Filter.ALLSERVICES.setRoot(viewtree));
filters.put(Filter.ALLSERVICES.getName(), Filter.ALLSERVICES);
addTree("All tags", GraphTree.TAGSROOT);
}
public void configure(PropertiesManager pm) {
started = false;
if(pm.rrddir == null) {
log(Level.ERROR, "Probes directory not configured, can't configure");
return;
}
if(pm.configdir == null) {
log(Level.ERROR, "Configuration directory not configured, can't configure");
return;
}
pm.configureStores();
setTimeout(pm.timeout);
setStep(pm.step);
log(Level.TRACE, "timers to build %s", pm.timers);
for(Map.Entry<String, TimerInfo> e: pm.timers.entrySet()) {
jrds.starter.Timer t = new jrds.starter.Timer(e.getKey(), e.getValue());
t.setParent(this);
timers.put(e.getKey(), t);
}
log(Level.DEBUG, "timers %s", timers);
renderer = new Renderer(50, pm.tmpdir);
log(Level.DEBUG, "Starting parsing descriptions");
ConfigObjectFactory conf = new ConfigObjectFactory(pm);
conf.setArchiveSetMap();
conf.setGraphDescMap();
Map<String, ProbeDesc<?>> allProbeDesc = conf.setProbeDescMap();
for(ProbeDesc<?> pd: allProbeDesc.values()) {
for(ProbeMeta meta: ArgFactory.enumerateAnnotation(pd.getProbeClass(), ProbeMeta.class, StarterNode.class)) {
log(Level.TRACE, "new probe meta: %s", meta);
daList.add(meta.discoverAgent());
}
}
log(Level.DEBUG, "Discover agents classes are %s", daList);
conf.setMacroMap();
for(Listener<?, ?> l: conf.setListenerMap().values()) {
registerStarter(l);
topStarters.add(l);
}
Set<String> hostsTags = new HashSet<String>();
Map<String, HostInfo> allHosts = conf.setHostMap(timers);
hostList.addAll(allHosts.values());
Set<Class<? extends Starter>> topStarterClasses = new HashSet<Class<? extends Starter>>();
// We try to load top level starter defined in probes
for(jrds.starter.Timer timer: timers.values()) {
Set<Class<? extends Starter>> timerStarterClasses = new HashSet<Class<? extends Starter>>();
for(HostStarter host: timer.getAllHosts()) {
hostsTags.addAll(host.getTags());
host.configureStarters(pm);
for(Probe<?, ?> p: host.getAllProbes()) {
p.configureStarters(pm);
try {
for(ProbeMeta meta: ArgFactory.enumerateAnnotation(p.getClass(), ProbeMeta.class, StarterNode.class)) {
log(Level.TRACE, "another probe meta: %s", meta);
timerStarterClasses.add(meta.timerStarter());
topStarterClasses.add(meta.topStarter());
}
} catch (Exception e) {
log(Level.ERROR, e, "Error inserting probe " + p + ": " + e.getMessage());
}
}
}
log(Level.DEBUG, "timer starters added %s for timer %s", timerStarterClasses, timer.getName());
for(Class<? extends Starter> starterClass: timerStarterClasses) {
try {
timer.registerStarter(starterClass.newInstance());
} catch (Throwable e) {
log(Level.ERROR, e, "Starter %s failed to register for timer %s: %s", starterClass, timer.getName(), e.getMessage());
}
}
timer.configureStarters(pm);
}
log(Level.DEBUG, "top starters added %s", topStarterClasses);
for(Class<? extends Starter> starterClass: topStarterClasses) {
try {
Starter top = starterClass.newInstance();
topStarters.add(top);
registerStarter(top);
} catch (Throwable e) {
log(Level.ERROR, e, "Top level starter %s failed to register: %s", starterClass, e.getMessage());
}
}
for(HostInfo host: hostList) {
for(Probe<?, ?> p: host.getProbes()) {
addProbe(p);
// Some probe are done outside of a starter
// Don't forget them
if(p.getHostList() == null) {
p.setParent(this);
}
}
}
// Configure the default ACL of all automatic filters
for(Filter filter: filters.values()) {
filter.addACL(pm.defaultACL);
}
Set<Tab> allTabs = new HashSet<Tab>();
// Let's build the tab for all the tags
doTagsTabs(hostsTags, allTabs);
// Let's build the tab with all the filters
doFilterTabs(conf.setFilterMap(), allTabs);
// Build all the sums and add them to all the graphs
doSums(conf.setSumMap(), graphMap, treeMap, allTabs);
// Let's build the tab with all the custom graph and add them to all
// graph
doCustomGraphs(conf.setGrapMap(), graphMap, treeMap, allTabs);
// Resolve the custom tabs and generate the associated tree
Map<String, Tab> customTabMap = conf.setTabMap();
doCustomTabs(customTabMap, treeMap, allTabs);
// Add the always here tabs
allTabs.add(new Tab.StaticTree("All services", PropertiesManager.SERVICESTAB, getGraphTreeByView().getByPath(GraphTree.VIEWROOT, "Services")));
allTabs.add(new Tab.StaticTree("All hosts", PropertiesManager.HOSTSTAB, getGraphTreeByHost()));
allTabs.add(new Tab.StaticTree("All views", PropertiesManager.VIEWSTAB, getGraphTreeByView()));
allTabs.add(new Tab("Administration", PropertiesManager.ADMINTAB) {
@Override
public String getJSCallback() {
return "adminTabCallback";
}
});
firstTab = makeTabs(pm.tabsList, allTabs, customTabMap, tabs);
// Hosts list adopts all tabs
for(Tab t: tabs.values()) {
t.setHostlist(this);
}
if(pm.security) {
for(GraphNode gn: graphMap.values()) {
gn.addACL(pm.defaultACL);
checkRoles(gn, GraphTree.HOSTROOT, gn.getTreePathByHost());
checkRoles(gn, GraphTree.VIEWROOT, gn.getTreePathByView());
}
}
started = true;
}
public void startTimers() {
if(started)
collectTimer = new Timer("jrds-main-timer/" + thisgeneration, true);
for(jrds.starter.Timer t: timers.values()) {
t.startTimer(collectTimer);
}
for(Starter s: this.topStarters) {
s.doStart();
}
}
/**
* Ensure that all collects are stopped, some slow probes might need a
* little help
*/
public void stop() {
started = false;
if(collectTimer != null)
collectTimer.cancel();
collectTimer = null;
for(Starter s: topStarters) {
s.doStop();
}
for(jrds.starter.Timer t: timers.values()) {
t.stopCollect();
for(HostStarter h: t.getAllHosts()) {
h.stopCollect();
for(Probe<?, ?> p: h.getAllProbes()) {
p.stopCollect();
}
}
t.interrupt();
}
}
String makeTabs(List<String> tabsList, Set<Tab> moretabs, Map<String, Tab> customTabMap, Map<String, Tab> tabs) {
Map<String, Tab> tabsmap = new HashMap<String, Tab>(moretabs.size());
for(Tab t: moretabs) {
if(t != null)
tabsmap.put(t.getId(), t);
}
log(Level.TRACE, "Looking for tabs list %s in %s", tabsList, moretabs);
String firstTab = null;
for(String tabid: tabsList) {
// @ is a magic place holder, used to replace if with the custom
// tabs list
if("@".equals(tabid)) {
for(Tab t: customTabMap.values()) {
tabs.put(t.getId(), t);
}
} else {
Tab t = tabsmap.get(tabid);
if(t != null) {
tabs.put(tabid, t);
// store the first tab
if(firstTab == null)
firstTab = tabid;
} else {
// Not a problem, some automatic tab might be empty (sum,
// customgraph, tags)
log(Level.DEBUG, "Non existent tab to add: " + tabid);
}
}
}
return firstTab;
}
void doTagsTabs(Set<String> hostsTags, Set<Tab> tabs) {
Tab tagsTab = new Tab.Filters("All tags", PropertiesManager.TAGSTAB);
for(String tag: hostsTags) {
Filter f = new FilterTag(tag);
filters.put(f.getName(), f);
tagsTab.add(f.getName());
}
tabs.add(tagsTab);
}
void doFilterTabs(Map<String, Filter> f, Set<Tab> tabs) {
Tab filterTab = new Tab.Filters("All filters", PropertiesManager.FILTERTAB);
for(Filter filter: f.values()) {
addFilter(filter);
filterTab.add(filter.getName());
}
tabs.add(filterTab);
}
void doSums(Map<String, Sum> sums, Map<Integer, GraphNode> graphMap, Map<String, GraphTree> treeMap, Set<Tab> tabs) {
// Let's build the tab with all the sums
if(sums.size() > 0) {
Tab sumGraphsTab = new Tab.DynamicTree("Sums", PropertiesManager.SUMSTAB);
sumGraphsTab.setHostlist(this);
for(Sum s: sums.values()) {
try {
s.configure(this);
graphMap.put(s.getQualifiedName().hashCode(), s);
sumGraphsTab.add(s.getQualifiedName(), "Sums", s.getName());
} catch (Exception e1) {
log(Level.ERROR, e1, "failed sum: %s", e1);
}
}
GraphTree tree = sumGraphsTab.getGraphTree();
treeMap.put(tree.getName(), tree);
tabs.add(sumGraphsTab);
}
}
void doCustomGraphs(Map<String, GraphDesc> graphs, Map<Integer, GraphNode> graphMap, Map<String, GraphTree> treeMap, Set<Tab> tabs) {
log(Level.DEBUG, "Parsing graphs configuration");
// Let's build the tab with all the custom graphs
if(!graphs.isEmpty()) {
Tab customGraphsTab = new Tab.DynamicTree("Custom graphs", PropertiesManager.CUSTOMGRAPHTAB);
customGraphsTab.setHostlist(this);
for(GraphDesc gd: graphs.values()) {
AutonomousGraphNode gn = new AutonomousGraphNode(gd);
gn.configure(this);
graphMap.put(gn.getQualifiedName().hashCode(), gn);
customGraphsTab.add(gn.getQualifiedName(), Arrays.asList(new String[] { gd.getName() }));
}
GraphTree tree = customGraphsTab.getGraphTree();
treeMap.put(tree.getName(), tree);
tabs.add(customGraphsTab);
}
}
void doCustomTabs(Map<String, Tab> customTabMap, Map<String, GraphTree> treeMap, Set<Tab> tabs) {
if(!customTabMap.isEmpty()) {
log(Level.DEBUG, "Tabs to add: %s", customTabMap.values());
for(Tab t: customTabMap.values()) {
t.setHostlist(this);
GraphTree tabtree = t.getGraphTree();
if(tabtree != null)
treeMap.put(t.getName(), tabtree);
}
}
}
public Collection<HostInfo> getHosts() {
return hostList;
}
public Set<String> getTabsId() {
return tabs.keySet();
}
public Tab getTab(String id) {
return tabs.get(id);
}
/**
* Create a new graph tree
*
* @param label The name of the tree
* @param root The name of the first element in the tree
* @return a graph tree
*/
private GraphTree addTree(String label, String root) {
if(!treeMap.containsKey(label)) {
GraphTree newTree = GraphTree.makeGraph(root);
treeMap.put(label, newTree);
return newTree;
}
return null;
}
public void addGraphs(Collection<GraphNode> graphs) {
for(GraphNode currGraph: graphs) {
LinkedList<String> path;
path = currGraph.getTreePathByHost();
getGraphTreeByHost().addGraphByPath(path, currGraph);
path = currGraph.getTreePathByView();
getGraphTreeByView().addGraphByPath(path, currGraph);
graphMap.put(currGraph.hashCode(), currGraph);
}
}
/**
* Generate the list of roles that might view this node, using the filters
*
* @param gn
* @param pathList
*/
private void checkRoles(GraphNode gn, String root, List<String> pathList) {
StringBuilder path = new StringBuilder("/" + root);
for(String pathElem: pathList) {
path.append('/').append(pathElem);
}
for(Filter f: filters.values()) {
if(f.acceptGraph(gn, path.toString())) {
log(Level.TRACE, "Adding ACL %s to %s", f.getACL(), gn);
gn.addACL(f.getACL());
}
}
}
public void addHost(HostInfo newhost) {
hostList.add(newhost);
}
public GraphTree getGraphTree(String name) {
return treeMap.get(name);
}
public Set<String> getTreesName() {
return treeMap.keySet();
}
public Collection<GraphTree> getTrees() {
return treeMap.values();
}
public GraphTree getGraphTreeByHost() {
return treeMap.get(Filter.ALLHOSTS.getName());
}
public GraphTree getGraphTreeByView() {
return treeMap.get(Filter.ALLVIEWS.getName());
}
/**
* Return a graph identified by his hash value
*
* @param id the hash value of the graph
* @return the graph found or null of nothing found
*/
public GraphNode getGraphById(int id) {
return graphMap.get(id);
}
/**
* Return a probe identified by his hash value
*
* @param id the hash value of the probe
* @return the probe found or null of nothing found
*/
public Probe<?, ?> getProbeById(int id) {
return probeMap.get(id);
}
/**
* Return a probe identified by path
*
* @param host the host
* @param probeName the probe name: the probeName element of a probedesc
* @return the graph found or null of nothing found
*/
public Probe<?, ?> getProbeByPath(String host, String probeName) {
String path = host + "/" + probeName;
return probeMap.get(path.hashCode());
}
public void addProbe(Probe<?, ?> p) {
probeMap.put(p.hashCode(), p);
addGraphs(p.getGraphList());
}
public GraphTree getNodeById(int id) {
GraphTree node = null;
for(GraphTree tree: treeMap.values()) {
node = tree.getById(id);
if(node != null)
return node;
}
return node;
}
private void addFilter(Filter newFilter) {
filters.put(newFilter.getName(), newFilter);
ACL acl = newFilter.getACL();
if(acl instanceof RolesACL) {
roles.addAll(((RolesACL) acl).getRoles());
}
log(Level.DEBUG, "Filter %s added with ACL %s", newFilter.getName(), newFilter.getACL());
}
public Filter getFilter(String name) {
Filter retValue = null;
if(name != null)
retValue = filters.get(name);
return retValue;
}
@Override
public String toString() {
return getClass().getName();
}
/**
* @return the renderer
*/
public Renderer getRenderer() {
return renderer;
}
/*
* (non-Javadoc)
*
* @see jrds.starter.StarterNode#isCollectRunning()
*/
@Override
public boolean isCollectRunning() {
return started;
}
/**
* @return the roles
*/
public Set<String> getRoles() {
return roles;
}
/**
* @return the defaultRoles
*/
public Set<String> getDefaultRoles() {
return defaultRoles;
}
public Set<DiscoverAgent> getDiscoverAgent() {
Set<DiscoverAgent> daSet = new HashSet<DiscoverAgent>(daList.size());
for(Class<? extends DiscoverAgent> daa: daList) {
try {
daSet.add(daa.newInstance());
} catch (Exception e) {
log(Level.ERROR, e, "Error creating discover agent " + daa.getName() + ": " + e);
}
}
return daSet;
}
/**
* @return the first tab
*/
public String getFirstTab() {
return firstTab;
}
/**
* @return the timers
*/
public Iterable<jrds.starter.Timer> getTimers() {
return timers.values();
}
/**
* @return the thisgeneration
*/
public int getGeneration() {
return thisgeneration;
}
}