Skip to content

Commit 1b6de2d

Browse files
authored
Merge pull request #19 from phillipivan/main
[feat] getOID action, with poll interval config option
2 parents b0b9561 + f859195 commit 1b6de2d

10 files changed

+6341
-4830
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.vscode/
22
node_modules/
3+
.yarn/
34
package-lock.json
45
/pkg
56
/pkg.tgz

.yarnrc.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodeLinker: node-modules

companion/HELP.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ To configure this module you will need:
99
1. Agent Address - The IP Address of the SNMP enabled device you want to control
1010
2. UDP Port - The number of the UDP port on which the agent is listening (defaults to port 161)
1111
3. SNMP Version - The version of SNMP used by the agent. This will dictate how this module will authenticate with the agent
12+
4. Poll Interval - The poll interval for Get OID actions with update enabled.
1213

1314
#### SNMP versions v1/v2c
1415

@@ -41,6 +42,7 @@ If you selected SNMP version `v3` you will also need to configure the following:
4142

4243
You can perform the following actions with this module:
4344

45+
- Get OID value, return to custom variable
4446
- Set OID value to an OctetString
4547
- Set OID value to a Number. This includes the following SNMP Object Types:
4648
- Integer

eslint.config.mjs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { generateEslintConfig } from '@companion-module/tools/eslint/config.mjs'
2+
3+
const baseConfig = async () => {
4+
await generateEslintConfig({})
5+
}
6+
7+
const customConfig = [
8+
...baseConfig,
9+
{
10+
languageOptions: {
11+
sourceType: 'module',
12+
},
13+
},
14+
]
15+
16+
export default customConfig

package.json

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
{
22
"name": "generic-snmp",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"main": "src/index.js",
5-
"sourceType": "module",
5+
"type": "module",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"format": "prettier -w .",
99
"eslint": "eslint",
10-
"semantic-release": "semantic-release"
10+
"lint": "eslint",
11+
"semantic-release": "semantic-release",
12+
"package": "companion-module-build"
1113
},
1214
"dependencies": {
1315
"@companion-module/base": "~1.10.0",
14-
"net-snmp": "^3.12.1"
16+
"net-snmp": "^3.15.0"
1517
},
1618
"devDependencies": {
17-
"@companion-module/tools": "^2.0.0",
19+
"@companion-module/tools": "^2.0.4",
1820
"@semantic-release/changelog": "^6.0.3",
1921
"@semantic-release/git": "^10.0.1",
2022
"eslint": "^8.1.0",
@@ -25,5 +27,7 @@
2527
"repository": {
2628
"type": "git",
2729
"url": "git+https://github.com/bitfocus/companion-module-generic-snmp.git"
28-
}
30+
},
31+
"prettier": "@companion-module/tools/.prettierrc.json",
32+
"packageManager": "[email protected]"
2933
}

src/actions.js

+35
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,41 @@ export default async function (self) {
183183
self.setOid(oid, snmp.ObjectType.oid, value)
184184
},
185185
}
186+
actionDefs['getOID'] = {
187+
name: 'Get OID value',
188+
options: [
189+
{
190+
type: 'textinput',
191+
label: 'OID',
192+
id: 'oid',
193+
default: '',
194+
required: true,
195+
useVariables: true,
196+
regex: Regex.SOMETHING,
197+
},
198+
{
199+
type: 'custom-variable',
200+
label: 'Variable',
201+
id: 'variable',
202+
tooltip: 'Custom Variable that OID value is returned to',
203+
},
204+
{
205+
type: 'checkbox',
206+
label: 'Update',
207+
id: 'update',
208+
tooltip: 'Update each poll interval',
209+
default: false,
210+
},
211+
],
212+
callback: async ({ options }) => {
213+
self.getOid(await self.parseVariablesInString(options.oid), options.variable)
214+
},
215+
subscribe: async ({ options }) => {
216+
if (options.update) {
217+
self.getOid(await self.parseVariablesInString(options.oid), options.variable)
218+
}
219+
},
220+
}
186221

187222
self.setActionDefinitions(actionDefs)
188223
}

src/configs.js

+11
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,16 @@ export function getConfigFields() {
133133
default: '',
134134
isVisible: ({ version, securityLevel }) => version === 'v3' && securityLevel === 'authPriv',
135135
},
136+
{
137+
type: 'number',
138+
id: 'interval',
139+
label: 'Poll Interval',
140+
width: 6,
141+
min: 0,
142+
max: 3600,
143+
default: 0,
144+
required: true,
145+
tooltip: 'Set to 0 to turn polling off.',
146+
},
136147
]
137148
}

src/index.js

+41
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@ class Generic_SNMP extends InstanceBase {
1919
this.config = config
2020
this.updateActions()
2121
this.connectAgent()
22+
if (this.config.interval > 0) {
23+
this.pollOids()
24+
}
2225
}
2326

2427
async configUpdated(config) {
2528
this.config = config
29+
if (this.pollTimer) {
30+
clearTimeout(this.pollTimer)
31+
delete this.pollTimer
32+
}
2633
this.connectAgent()
34+
if (this.config.interval > 0) {
35+
this.pollOids()
36+
}
2737
}
2838

2939
connectAgent() {
@@ -117,8 +127,39 @@ class Generic_SNMP extends InstanceBase {
117127
})
118128
}
119129

130+
getOid(oid, customVariable) {
131+
try {
132+
this.session.get(
133+
[oid],
134+
((error, varbinds) => {
135+
if (error) {
136+
this.log('warn', `getOid error: ${JSON.stringify(error)} cannot set ${customVariable}`)
137+
return
138+
}
139+
//this.log('debug', `OID: ${varbinds[0].oid} value: ${varbinds[0].value} setting to: ${customVariable}`)
140+
this.setCustomVariableValue(customVariable, varbinds[0].value)
141+
}).bind(this),
142+
)
143+
} catch (e) {
144+
this.log('warn', `getOid error: ${JSON.stringify(e)} cannot set ${customVariable}`)
145+
}
146+
}
147+
148+
pollOids() {
149+
this.subscribeActions('getOID')
150+
if (this.config.interval > 0) {
151+
this.pollTimer = setTimeout(() => {
152+
this.pollOids()
153+
}, this.config.interval * 1000)
154+
}
155+
}
156+
120157
async destroy() {
121158
this.log('debug', `destroy ${this.id}`)
159+
if (this.pollTimer) {
160+
clearTimeout(this.pollTimer)
161+
delete this.pollTimer
162+
}
122163
this.disconnectAgent()
123164
}
124165

src/upgrades.js

+16
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,20 @@ export default [
2828
}
2929
return result
3030
},
31+
function v210(_context, props) {
32+
const result = {
33+
updatedActions: [],
34+
updatedConfig: null,
35+
updatedFeedbacks: [],
36+
}
37+
if (props.config !== null) {
38+
let config = props.config
39+
if (config.interval == undefined || config.interval == null) {
40+
config.interval = 0
41+
result.updatedConfig = config
42+
}
43+
}
44+
45+
return result
46+
},
3147
]

0 commit comments

Comments
 (0)