2681bf899ef23a27e075ebac40bfb79c6ad197ff
[public/netxms.git] / src / agent / subagents / bind9 / src / main / java / com / github / tomaskir / netxms / subagents / bind9 / Bind9Plugin.java
1 package com.github.tomaskir.netxms.subagents.bind9;
2
3 import com.github.tomaskir.netxms.subagents.bind9.collection.CollectionResult;
4 import com.github.tomaskir.netxms.subagents.bind9.collection.Collector;
5 import com.github.tomaskir.netxms.subagents.bind9.exceptions.CollectionErrorException;
6 import org.netxms.agent.Config;
7 import org.netxms.agent.Parameter;
8 import org.netxms.agent.ParameterType;
9 import org.netxms.agent.Plugin;
10 import org.netxms.agent.adapters.ParameterAdapter;
11
12 import java.nio.file.Paths;
13 import java.util.HashSet;
14 import java.util.Set;
15 import java.util.concurrent.locks.ReadWriteLock;
16 import java.util.concurrent.locks.ReentrantReadWriteLock;
17
18 /**
19 * @author Tomas Kirnak
20 * @since 2.1-M1
21 */
22 public final class Bind9Plugin extends Plugin {
23
24 private static final String NAME = "bind9 Java Subagent Plugin";
25 private static final String VERSION = "2.1-M1";
26
27 private final Parameters supportedParameters = new Parameters();
28 private final ReadWriteLock dataCollectionLock = new ReentrantReadWriteLock();
29 private final CollectionResult collectionResult = new CollectionResult();
30
31 private final Collector collector;
32 private final Thread collectionThread;
33
34 // constructor
35 public Bind9Plugin(Config config) {
36 super(config);
37
38 // get config values
39 String statsFile = config.getValue("/bind9/StatisticsFile", "");
40 long collectionInterval = config.getValueLong("/bind9/CollectionInterval", -1);
41
42 // check if all config values present
43 if (statsFile.equals("")) {
44 throw new IllegalStateException("'statsFile' configuration value not found " +
45 "in 'bind9' section of Agent configuration");
46 }
47 if (collectionInterval < 1) {
48 throw new IllegalStateException("'collectionInterval' configuration value not found or lower than 1" +
49 "in 'bind9' section of Agent configuration");
50 }
51
52 this.collector = Collector.builder()
53 .statsFile(Paths.get(statsFile))
54 .collectionInterval(collectionInterval)
55 .supportedParameters(supportedParameters)
56 .result(collectionResult)
57 .dataCollectionLock(dataCollectionLock)
58 .build();
59
60 this.collectionThread = new Thread(collector);
61 }
62
63 @Override
64 public void init(Config config) {
65 super.init(config);
66
67 // start the collection thread
68 collectionThread.start();
69 }
70
71 @Override
72 public void shutdown() {
73 super.shutdown();
74
75 // signal the collection thread to terminate
76 collector.terminate();
77
78 try {
79 collectionThread.join();
80 } catch (InterruptedException ignored) {
81 }
82 }
83
84 @Override
85 public Parameter[] getParameters() {
86 Set<Parameter> parameterSet = new HashSet<>();
87
88 // create parameters for all supported attributes
89 for (String[] entry : supportedParameters.getList()) {
90 final String parameterName = entry[0];
91 final String parameterDescription = entry[1];
92
93 parameterSet.add(
94 new ParameterAdapter(parameterName, parameterDescription, ParameterType.UINT64) {
95 @Override
96 public String getValue(String s) throws Exception {
97 // lock data collection lock for reading
98 dataCollectionLock.readLock().lock();
99
100 // if collection error, throw so Agent interprets it as a collection error
101 if (collectionResult.isCollectionError())
102 throw new CollectionErrorException();
103
104 String result = collectionResult.getResult().get(parameterName);
105
106 // unlock data collection lock
107 dataCollectionLock.readLock().unlock();
108
109 // if result not present in the result map, its value is 0 (bind9 stats behave this way)
110 if (result == null)
111 result = "0";
112
113 return result;
114 }
115 }
116 );
117 }
118
119 // return the parameters as an array
120 return parameterSet.toArray(new Parameter[parameterSet.size()]);
121 }
122
123 @Override
124 public String getName() {
125 return NAME;
126 }
127
128 @Override
129 public String getVersion() {
130 return VERSION;
131 }
132
133 }