1 package com
.github
.tomaskir
.netxms
.subagents
.bind9
;
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
;
12 import java
.nio
.file
.Paths
;
13 import java
.util
.HashSet
;
15 import java
.util
.concurrent
.locks
.ReadWriteLock
;
16 import java
.util
.concurrent
.locks
.ReentrantReadWriteLock
;
19 * @author Tomas Kirnak
22 public final class Bind9Plugin
extends Plugin
{
24 private static final String NAME
= "bind9 Java Subagent Plugin";
25 private static final String VERSION
= "2.1-M1";
27 private final Parameters supportedParameters
= new Parameters();
28 private final ReadWriteLock dataCollectionLock
= new ReentrantReadWriteLock();
29 private final CollectionResult collectionResult
= new CollectionResult();
31 private final Collector collector
;
32 private final Thread collectionThread
;
35 public Bind9Plugin(Config config
) {
39 String statsFile
= config
.getValue("/bind9/StatisticsFile", "");
40 long collectionInterval
= config
.getValueLong("/bind9/CollectionInterval", -1);
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");
47 if (collectionInterval
< 1) {
48 throw new IllegalStateException("'collectionInterval' configuration value not found or lower than 1" +
49 "in 'bind9' section of Agent configuration");
52 this.collector
= Collector
.builder()
53 .statsFile(Paths
.get(statsFile
))
54 .collectionInterval(collectionInterval
)
55 .supportedParameters(supportedParameters
)
56 .result(collectionResult
)
57 .dataCollectionLock(dataCollectionLock
)
60 this.collectionThread
= new Thread(collector
);
64 public void init(Config config
) {
67 // start the collection thread
68 collectionThread
.start();
72 public void shutdown() {
75 // signal the collection thread to terminate
76 collector
.terminate();
79 collectionThread
.join();
80 } catch (InterruptedException ignored
) {
85 public Parameter
[] getParameters() {
86 Set
<Parameter
> parameterSet
= new HashSet
<>();
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];
94 new ParameterAdapter(parameterName
, parameterDescription
, ParameterType
.UINT64
) {
96 public String
getValue(String s
) throws Exception
{
97 // lock data collection lock for reading
98 dataCollectionLock
.readLock().lock();
100 // if collection error, throw so Agent interprets it as a collection error
101 if (collectionResult
.isCollectionError())
102 throw new CollectionErrorException();
104 String result
= collectionResult
.getResult().get(parameterName
);
106 // unlock data collection lock
107 dataCollectionLock
.readLock().unlock();
109 // if result not present in the result map, its value is 0 (bind9 stats behave this way)
119 // return the parameters as an array
120 return parameterSet
.toArray(new Parameter
[parameterSet
.size()]);
124 public String
getName() {
129 public String
getVersion() {