Skip to content

Commit

Permalink
Add filter for nodes (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanley288 authored Jul 7, 2016
1 parent 83f4865 commit de714b5
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 129 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
playground.js
lib/*
4 changes: 0 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
"extends": "airbnb",
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true,
"mocha": true,
},
"ecmaFeatures": {
"jsx": true,
"es6": true,
"classes": true,
},
"rules": {
"semi": [2, "never"],
Expand Down
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,21 @@ Nodes monitored by SolarWinds. `NODE` can be a **node id** or a **hostname**.

Options:

-h, --help output usage information
--filter <value> Filter output based on conditions provided
-h, --help output usage information
```

##### Filtering

The filtering flag format is "key=value".

The currently supported filters are:

- id
- name
- hostname
- ip

#### node inspect

```console
Expand Down Expand Up @@ -251,9 +263,20 @@ Virtual machines accessible by SolarWinds. `VM` can be a vm **id** or a vm **nam

Options:

-h, --help output usage information
--filter <value> Filter output based on conditions provided
-h, --help output usage information
```

##### Filtering

The filtering flag format is "key=value".

The currently supported filters are:

- id
- name
- ip

#### vm inspect

```console
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solarwinds",
"version": "0.3.1",
"version": "0.3.2",
"description": "The Node.js library and CLI for the Solarwinds API",
"main": "./lib/index.js",
"bin": {
Expand All @@ -10,7 +10,8 @@
"dev": "babel-node ./playground.js",
"dev:cli": "babel src/ --out-dir lib/ && node ./lib/solarwinds.js",
"build": "babel src/ --out-dir lib/",
"test": "snyk test && ava",
"lint": "eslint .",
"test": "npm run lint && snyk test && ava",
"test:unit": "ava",
"snyk:monitor": "snyk auth $SNYK_TOKEN && snyk monitor"
},
Expand All @@ -33,21 +34,21 @@
"commander": "^2.9.0",
"easy-table": "^1.0.0",
"is_js": "^0.8.0",
"moment": "^2.13.0",
"moment": "^2.14.1",
"parse-duration": "^0.1.1",
"request": "^2.72.0"
},
"devDependencies": {
"ava": "^0.15.2",
"babel-cli": "^6.10.1",
"babel-eslint": "^6.0.5",
"babel-eslint": "^6.1.0",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"eslint": "^2.13.1",
"eslint": "^3.0.1",
"eslint-config-airbnb": "^9.0.1",
"eslint-plugin-import": "^1.9.2",
"eslint-plugin-jsx-a11y": "^1.5.3",
"eslint-plugin-import": "^1.10.2",
"eslint-plugin-jsx-a11y": "^1.5.5",
"eslint-plugin-react": "^5.2.2",
"snyk": "^1.16.0"
},
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import is from 'is_js'

import Client from './client'
import Nodes from './nodes'
import VirtualMachines from './virtualMachines'
import NodeManager from './managers/nodeManager'
import VirtualMachineManager from './managers/virtualMachineManager'
import ApplicationTemplates from './applicationTemplates'
import Credentials from './credentials'

Expand All @@ -29,8 +29,8 @@ class Landscape {

this.client = new Client(username, password, hostname, port)

this.nodes = new Nodes(this.client)
this.virtualMachines = new VirtualMachines(this.client)
this.nodes = new NodeManager(this.client)
this.virtualMachines = new VirtualMachineManager(this.client)
this.applicationTemplates = new ApplicationTemplates(this.client)
this.credentials = new Credentials(this.client)
}
Expand Down
117 changes: 117 additions & 0 deletions src/managers/nodeManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import moment from 'moment'
import parse from 'parse-duration'
import is from 'is_js'

import Node from '../models/node'

class NodeManager {
props = Node.props.join()
table = 'Orion.Nodes'

constructor(client) {
this.client = client
}

async query(filter = {}) {
let nodes = []
if (is.not.empty(filter)) {
if (filter.hasOwnProperty('name')) {
nodes = await this.client.query(this.getFilterQuery('caption', filter.name.toLowerCase()))
} else if (filter.hasOwnProperty('ip')) {
nodes = await this.client.query(this.getFilterQuery('ip_address', filter.ip))
} else if (filter.hasOwnProperty('hostname')) {
nodes = await this.client.query(this.getFilterQuery('dns', filter.hostname.toLowerCase()))
} else if (filter.hasOwnProperty('id')) {
nodes = await this.client.query(this.getFilterQuery('nodeid', filter.id))
}
} else {
nodes = await this.client.query(`
SELECT ${this.props}
FROM ${this.table}
`)
}

return nodes.map(x => new Node(x))
}

async find(id) {
const res = await this.client.read(`Orion/${this.table}/NodeID=${id}`)

return new Node(res)
}

async findByName(name) {
const props = Node.props.join()
const res = await this.client.query(`
SELECT TOP 1 ${props}
FROM ${this.table}
WHERE SysName LIKE '%${name.toLowerCase()}%'
`)

return new Node(res[0])
}

async unmanage(id, duration) {
const start = moment.utc()
const end = moment.utc().add(parse(duration), 'ms')
const format = 'MM-DD-YYYY HH:mm:ss A'
const data = [`N:${id}`, start.format(format), end.format(format), 'false']

await this.client.invoke(`${this.table}/Unmanage`, data)

return this.find(id)
}

async remanage(id) {
const data = [`N:${id}`]
await this.client.invoke(`${this.table}/Remanage`, data)

return this.find(id)
}

async create(node) {
const data = {
EntityType: 'Orion.Nodes',
EngineID: 1,

// candidates for exposing as options
DynamicIP: false,
ObjectSubType: 'SNMP',
SNMPVersion: 2,
// SNMPPort: 161, // agentPort?
Allow64BitCounters: true,
PollInterval: 300,
RediscoveryInterval: 30,
StatCollection: 15,
External: false,

Caption: node.name, // same as DisplayName, NodeName
SysName: node.name,

DNS: node.hostname,

Community: node.community,

IPAddress: node.ip,
}

await this.client.create(this.table, data)

return node.name
}

remove(id) {
return this.client.delete(`Orion/${this.table}/NodeID=${id}`)
}

getFilterQuery(column, value) {
return `
SELECT ${this.props}
FROM ${this.table}
WHERE ${column} LIKE '%${value}%'
ORDER BY ${column}
`
}
}

export default NodeManager
22 changes: 12 additions & 10 deletions src/virtualMachines.js → src/managers/virtualMachineManager.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import VirtualMachine from './models/virtualMachine'
import is from 'is_js'

class VirtualMachines {
props = VirtualMachine.props.join()
import VirtualMachine from '../models/virtualMachine'

class VirtualMachineManager {
props = VirtualMachine.props.join()
table = 'Orion.VIM.VirtualMachines'

constructor(client) {
this.client = client
}

async query(filter) {
async query(filter = {}) {
let vms
if (filter) {
if (is.not.empty(filter)) {
if (filter.hasOwnProperty('name')) {
vms = await this.client.query(this.getQuery('name', filter.name.toLowerCase()))
} else if (filter.hasOwnProperty('ip')) {
vms = await this.client.query(this.getQuery('iPAddress', filter.ip))
vms = await this.client.query(this.getQuery('ipaddress', filter.ip))
} else {
vms = await this.client.query(this.getQuery('virtualMachineID', filter.id))
vms = await this.client.query(this.getQuery('virtualmachineid', filter.id))
}
} else {
vms = await this.client.query(`
SELECT ${this.props}
FROM ${this.table}
ORDER BY virtualMachineID
`)
`)
}

return vms.map(x => new VirtualMachine(x))
Expand Down Expand Up @@ -52,8 +54,8 @@ class VirtualMachines {
FROM ${this.table}
WHERE ${key} LIKE '%${value}%'
ORDER BY ${key}
`
`
}
}

export default VirtualMachines
export default VirtualMachineManager
1 change: 1 addition & 0 deletions src/models/applicationTemplate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ApplicationTemplate {
// eslint-disable-next-line max-len
static props = ['applicationTemplateID', 'name', 'isMockTemplate', 'created', 'lastModified', 'viewID', 'hasImportedView', 'customApplicationType', 'uniqueId', 'displayName', 'description', 'instanceType', 'uri']

constructor(appTemplate) {
Expand Down
1 change: 1 addition & 0 deletions src/models/credential.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Credential {
// eslint-disable-next-line max-len
static props = ['id', 'name', 'description', 'credentialType', 'credentialOwner', 'displayName', 'instanceType', 'uri']

constructor(credential) {
Expand Down
1 change: 1 addition & 0 deletions src/models/node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Node {
// eslint-disable-next-line max-len
static props = ['nodeID', 'objectSubType', 'iPAddress', 'iPAddressType', 'dynamicIP', 'caption', 'nodeDescription', 'description', 'dns', 'sysName', 'vendor', 'sysObjectID', 'location', 'contact', 'vendorIcon', 'icon', 'status', 'statusLED', 'statusDescription', 'customStatus', 'iOSImage', 'iOSVersion', 'groupStatus', 'statusIcon', 'lastBoot', 'systemUpTime', 'responseTime', 'percentLoss', 'avgResponseTime', 'minResponseTime', 'maxResponseTime', 'cPULoad', 'memoryUsed', 'memoryAvailable', 'percentMemoryUsed', 'percentMemoryAvailable', 'lastSync', 'lastSystemUpTimePollUtc', 'machineType', 'isServer', 'severity', 'uiSeverity', 'childStatus', 'allow64BitCounters', 'agentPort', 'totalMemory', 'cmts', 'customPollerLastStatisticsPoll', 'customPollerLastStatisticsPollSuccess', 'sNMPVersion', 'pollInterval', 'engineID', 'rediscoveryInterval', 'nextPoll', 'nextRediscovery', 'statCollection', 'external', 'community', 'rWCommunity', 'ip', 'ipAddress', 'iPAddressGUID', 'nodeName', 'blockUntil', 'orionIdPrefix', 'orionIdColumn', 'skippedPollingCycles', 'minutesSinceLastSync', 'entityType', 'detailsUrl', 'displayName', 'unManaged', 'unManageFrom', 'unManageUntil', 'image', 'statusIconHint', 'instanceType', 'uri']

constructor(node) {
Expand Down
1 change: 1 addition & 0 deletions src/models/virtualMachine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class VirtualMachine {
// eslint-disable-next-line max-len
static props = ['virtualMachineID', 'managedObjectID', 'uuid', 'hostID', 'nodeID', 'resourcePoolID', 'vMConfigFile', 'memoryConfigured', 'memoryShares', 'cPUShares', 'guestState', 'iPAddress', 'logDirectory', 'guestVmWareToolsVersion', 'guestVmWareToolsStatus', 'name', 'guestName', 'guestFamily', 'guestDnsName', 'nicCount', 'vDisksCount', 'processorCount', 'powerState', 'bootTime', 'configStatus', 'overallStatus', 'nodeStatus', 'networkUsageRate', 'networkTransmitRate', 'networkReceiveRate', 'cpuLoad', 'cpuUsageMHz', 'memUsage', 'memUsageMB', 'isLicensed', 'platformID', 'pollingSource', 'relativePath', 'datastoreIdentifier', 'cpuReady', 'swappedMemoryUtilization', 'swappedMemoryUtilizationPercent', 'balloonMemload', 'balloonMemloadPercent', 'iOPSTotal', 'iOPSRead', 'iOPSWrite', 'latencyTotal', 'latencyRead', 'latencyWrite', 'snapshotStorageSize', 'consumedMemLoad', 'consumedPercentMemLoad', 'lastActivityDate', 'totalStorageSize', 'totalStorageSizeUsed', 'volumeSummaryCapacity', 'volumeSummaryFreeSpace', 'volumeSummaryFreeSpacePercent', 'volumeSummaryCapacityDepletionDate', 'triggeredAlarmDescription', 'orionIdPrefix', 'orionIdColumn', 'runTime', 'cpuCostop', 'dynamicMemoryEnabled', 'snapshotSummaryCount', 'dateCreated', 'oldestSnapshotDate', 'heartBeat', 'snapshotDateModified', 'memoryAllocationLimit', 'virtualDiskDateModified', 'detailsUrl', 'status', 'statusDescription', 'statusLED', 'unManaged', 'unManageFrom', 'unManageUntil', 'image', 'ancestorDisplayNames', 'ancestorDetailsUrls', 'statusIconHint', 'displayName', 'description', 'instanceType', 'uri']

constructor(vm) {
Expand Down
Loading

0 comments on commit de714b5

Please sign in to comment.