Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ Deactivates a host object.
### GET: /host
Retrieves a host object.

### GET: /registered
List the hosts registred in NagiosQL.

### GET: /list
List the hosts in the specified status(deactive or active).

### POST: /host
Creates a new host object.

Expand Down
73 changes: 66 additions & 7 deletions lib/nogql.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var ini = require('ini');
var mysql = require('mysql');
var pfinder = require('portfinder');
var q = require('q');
var moment = require('moment')


// setup logger
Expand Down Expand Up @@ -159,9 +160,9 @@ var NogQLServer = exports.NogQLServer = function(options) {
}

var host_name = url[2];
query('SELECT id, config_id, host_name, address, check_command, active FROM tbl_host WHERE host_name=?', [host_name]).then(function (results) {
query('SELECT id, config_id, host_name, address, check_command, active, notes FROM tbl_host WHERE host_name=?', [host_name]).then(function (results) {
if (results[0].length != 1) {
throw new Error('could not retrieve configuration for requested host');
sendError(404, 'could not retrieve configuration for requested host');
}

result = results[0][0];
Expand Down Expand Up @@ -225,13 +226,17 @@ var NogQLServer = exports.NogQLServer = function(options) {
}

template_id = results[0][0].id;

var formatted_now = moment().format('YYYY-MM-DD HH:mm:ss');
var host_row = {
active: 0, config_id: domain_id,
host_name: data.host_name, alias: data.host_name, address: data.address,
active: '0', config_id: domain_id,
host_name: data.host_name, alias: data.host_name,
display_name: data.host_name, address: data.address,
check_command: command_id, check_period: 22, max_check_attempts: 3,
notification_interval: 30, notification_options: 'd,u,r', notification_period: 22,
use_template: 1
use_template: 1, notes: data.notes, initial_state: 'o', action_url: '',
flap_detection_options: '',stalking_options: '', notes_url: '',
icon_image:'', icon_image_alt: '', vrml_image: '',statusmap_image:'',
'2d_coords': '', '3d_coords': '', name: '', last_modified: formatted_now
};
return query('INSERT INTO tbl_host SET ?', host_row);

Expand All @@ -257,7 +262,61 @@ var NogQLServer = exports.NogQLServer = function(options) {
sendError(405, 'request of invalid type to end-point');
}
break;

case 'registered':
// only supports get
if (req.method === 'GET') {
query('SELECT id, host_name, address FROM tbl_host WHERE register=\'1\'').then(function (results) {
if (results[0].length < 1) {
sendError(404, 'could not retrieve configuration for requested state');
return;
}
if (req.method === 'GET') {
sendResponse(200, results[0]);
return;
}
}).catch(function (error) {
sendError(500, error.message);
}).done();
} else {
sendError(405, 'request of invalid type to end-point');
}
break;
case 'list':
// list function only supports get
if (!url[2]) {
sendError(400, 'request does not contain a valid status');
return;
}
var qlstatus = url[2];
var search_filter = '';
switch (qlstatus) {
case 'active':
search_filter = '1';
break;
case 'deactive':
search_filter = '0';
break;
default:
sendError(400, 'request does not contain a valid status(active/deactive)');
return;
}
if (req.method === 'GET') {
query('SELECT id, host_name, address FROM tbl_host WHERE active=?', search_filter).then(function (results) {
if (results[0].length < 1) {
sendError(404, 'could not retrieve configuration for requested state');
return;
}
if (req.method === 'GET') {
sendResponse(200, results[0]);
return;
}
}).catch(function (error) {
sendError(500, error.message);
}).done();
} else {
sendError(405, 'request of invalid type to end-point');
}
break;
default:
sendError(405, 'request to an invalid end-point');
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"portfinder": "~0.2.1",
"q": "1.0.x",
"winston": "0.7.x",
"yargs": "1.2.x"
"yargs": "1.2.x",
"moment": "2.22.x"
},
"engines": [
"node >= 0.10"
Expand Down