1 package com
.github
.tomaskir
.netxms
.subagents
.bind9
.collection
;
3 import com
.github
.tomaskir
.netxms
.subagents
.bind9
.Parameters
;
4 import com
.github
.tomaskir
.netxms
.subagents
.bind9
.exceptions
.StatsFileRemovalException
;
6 import org
.netxms
.agent
.SubAgent
;
8 import java
.io
.IOException
;
9 import java
.nio
.charset
.StandardCharsets
;
10 import java
.nio
.file
.Files
;
11 import java
.nio
.file
.NoSuchFileException
;
12 import java
.nio
.file
.Path
;
13 import java
.util
.List
;
14 import java
.util
.concurrent
.locks
.ReadWriteLock
;
17 * @author Tomas Kirnak
20 public final class Collector
implements Runnable
{
22 private final long collectionInterval
;
23 private final Path statsFile
;
24 private final Parameters supportedParameters
;
25 private final CollectionResult result
;
26 private final ReadWriteLock dataCollectionLock
;
28 private long lastCollection
= 0;
29 private boolean threadRunning
= true;
33 public Collector(long collectionInterval
, Path statsFile
, Parameters supportedParameters
,
34 CollectionResult result
, ReadWriteLock dataCollectionLock
) {
35 this.collectionInterval
= collectionInterval
;
36 this.statsFile
= statsFile
;
37 this.supportedParameters
= supportedParameters
;
39 this.dataCollectionLock
= dataCollectionLock
;
42 public void terminate() {
43 threadRunning
= false;
52 while (threadRunning
) {
53 now
= System
.currentTimeMillis() / 1000;
55 if (now
> lastCollection
+ collectionInterval
) {
56 // remove existing statistics file
59 } catch (StatsFileRemovalException e
) {
60 SubAgent
.writeLog(SubAgent
.LogLevel
.WARNING
,
61 "Failed to delete bind9 statistics file, error: '" + e
.getCause().getMessage() + "'");
63 result
.setCollectionError(true);
64 updateLastCollectionTime();
68 // run "rndc stats" to generate new statistics file
70 rndc
= Runtime
.getRuntime().exec("rndc stats");
71 } catch (IOException e
) {
72 SubAgent
.writeLog(SubAgent
.LogLevel
.ERROR
,
73 "Failed to run 'rndc stats', error: '" + e
.getMessage() + "'");
75 result
.setCollectionError(true);
76 updateLastCollectionTime();
80 // wait for "rndc stats" to finish
83 } catch (InterruptedException ignored
) {
89 if (rndc
.exitValue() != 0) {
90 SubAgent
.writeLog(SubAgent
.LogLevel
.ERROR
,
91 "'rndc stats' exited with a non-0 exit code value");
93 result
.setCollectionError(true);
94 updateLastCollectionTime();
98 // load stats from the stats file
100 lines
= Files
.readAllLines(statsFile
, StandardCharsets
.UTF_8
);
101 } catch (IOException e
) {
102 SubAgent
.writeLog(SubAgent
.LogLevel
.WARNING
,
103 "Unable to read bind9 statistics file, error: '" + e
.getMessage() + "'");
105 result
.setCollectionError(true);
106 updateLastCollectionTime();
110 // lock data collection lock for writing
111 dataCollectionLock
.writeLock().lock();
113 // remove previous values in result
114 result
.getResult().clear();
116 // for every parameter supported by this plugin
117 for (String
[] entry
: supportedParameters
.getList()) {
119 for (String line
: lines
) {
120 int index
= line
.indexOf(entry
[2]);
122 // if attribute found
124 // get value of the attribute
125 String value
= line
.substring(0, index
).trim();
127 // insert into the result map
128 result
.getResult().put(entry
[0], value
);
135 result
.setCollectionError(false);
136 updateLastCollectionTime();
138 // unlock data collection lock
139 dataCollectionLock
.writeLock().unlock();
144 } catch (InterruptedException ignored
) {
149 private void statsFileCleanup() throws StatsFileRemovalException
{
151 Files
.delete(statsFile
);
152 } catch (NoSuchFileException ignored
) {
153 // if the file doesn't exist, we do nothing
154 } catch (IOException e
) {
155 throw new StatsFileRemovalException(e
);
159 private void updateLastCollectionTime() {
160 lastCollection
= System
.currentTimeMillis() / 1000;