Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] getOID action, with poll interval config option #19

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.vscode/
node_modules/
.yarn/
package-lock.json
/pkg
/pkg.tgz
Expand Down
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
2 changes: 2 additions & 0 deletions companion/HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ To configure this module you will need:
1. Agent Address - The IP Address of the SNMP enabled device you want to control
2. UDP Port - The number of the UDP port on which the agent is listening (defaults to port 161)
3. SNMP Version - The version of SNMP used by the agent. This will dictate how this module will authenticate with the agent
4. Poll Interval - The poll interval for Get OID actions with update enabled.

#### SNMP versions v1/v2c

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

You can perform the following actions with this module:

- Get OID value, return to custom variable
- Set OID value to an OctetString
- Set OID value to a Number. This includes the following SNMP Object Types:
- Integer
Expand Down
16 changes: 16 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { generateEslintConfig } from '@companion-module/tools/eslint/config.mjs'

const baseConfig = async () => {
await generateEslintConfig({})
}

const customConfig = [
...baseConfig,
{
languageOptions: {
sourceType: 'module',
},
},
]

export default customConfig
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"name": "generic-snmp",
"version": "2.0.0",
"version": "2.1.0",
"main": "src/index.js",
"sourceType": "module",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"format": "prettier -w .",
"eslint": "eslint",
"semantic-release": "semantic-release"
"lint": "eslint",
"semantic-release": "semantic-release",
"package": "companion-module-build"
},
"dependencies": {
"@companion-module/base": "~1.10.0",
"net-snmp": "^3.12.1"
"net-snmp": "^3.15.0"
},
"devDependencies": {
"@companion-module/tools": "^2.0.0",
"@companion-module/tools": "^2.0.4",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"eslint": "^8.1.0",
Expand All @@ -25,5 +27,7 @@
"repository": {
"type": "git",
"url": "git+https://github.com/bitfocus/companion-module-generic-snmp.git"
}
},
"prettier": "@companion-module/tools/.prettierrc.json",
"packageManager": "yarn@4.4.1"
}
35 changes: 35 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,41 @@ export default async function (self) {
self.setOid(oid, snmp.ObjectType.oid, value)
},
}
actionDefs['getOID'] = {
name: 'Get OID value',
options: [
{
type: 'textinput',
label: 'OID',
id: 'oid',
default: '',
required: true,
useVariables: true,
regex: Regex.SOMETHING,
},
{
type: 'custom-variable',
label: 'Variable',
id: 'variable',
tooltip: 'Custom Variable that OID value is returned to',
},
{
type: 'checkbox',
label: 'Update',
id: 'update',
tooltip: 'Update each poll interval',
default: false,
},
],
callback: async ({ options }) => {
self.getOid(await self.parseVariablesInString(options.oid), options.variable)
},
subscribe: async ({ options }) => {
if (options.update) {
self.getOid(await self.parseVariablesInString(options.oid), options.variable)
}
},
}

self.setActionDefinitions(actionDefs)
}
11 changes: 11 additions & 0 deletions src/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,16 @@ export function getConfigFields() {
default: '',
isVisible: ({ version, securityLevel }) => version === 'v3' && securityLevel === 'authPriv',
},
{
type: 'number',
id: 'interval',
label: 'Poll Interval',
width: 6,
min: 0,
max: 3600,
default: 0,
required: true,
tooltip: 'Set to 0 to turn polling off.',
},
]
}
41 changes: 41 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@ class Generic_SNMP extends InstanceBase {
this.config = config
this.updateActions()
this.connectAgent()
if (this.config.interval > 0) {
this.pollOids()
}
}

async configUpdated(config) {
this.config = config
if (this.pollTimer) {
clearTimeout(this.pollTimer)
delete this.pollTimer
}
this.connectAgent()
if (this.config.interval > 0) {
this.pollOids()
}
}

connectAgent() {
Expand Down Expand Up @@ -117,8 +127,39 @@ class Generic_SNMP extends InstanceBase {
})
}

getOid(oid, customVariable) {
try {
this.session.get(
[oid],
((error, varbinds) => {
if (error) {
this.log('warn', `getOid error: ${JSON.stringify(error)} cannot set ${customVariable}`)
return
}
//this.log('debug', `OID: ${varbinds[0].oid} value: ${varbinds[0].value} setting to: ${customVariable}`)
this.setCustomVariableValue(customVariable, varbinds[0].value)
}).bind(this),
)
} catch (e) {
this.log('warn', `getOid error: ${JSON.stringify(e)} cannot set ${customVariable}`)
}
}

pollOids() {
this.subscribeActions('getOID')
if (this.config.interval > 0) {
this.pollTimer = setTimeout(() => {
this.pollOids()
}, this.config.interval * 1000)
}
}

async destroy() {
this.log('debug', `destroy ${this.id}`)
if (this.pollTimer) {
clearTimeout(this.pollTimer)
delete this.pollTimer
}
this.disconnectAgent()
}

Expand Down
16 changes: 16 additions & 0 deletions src/upgrades.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,20 @@ export default [
}
return result
},
function v210(_context, props) {
const result = {
updatedActions: [],
updatedConfig: null,
updatedFeedbacks: [],
}
if (props.config !== null) {
let config = props.config
if (config.interval == undefined || config.interval == null) {
config.interval = 0
result.updatedConfig = config
}
}

return result
},
]
Loading
Loading