Rocket.Chat SaaS
Nixstats notification
Add Nixstats notifications via a new WebHook in Rocket.Chat
  1. 1.
    In Rocket.Chat go to "Administration"->"Integrations" and create "New Integration".
  2. 2.
    Choose Incoming WebHook.
  3. 3.
    Follow all instructions like Enable, give it a name, link to channel etc. Set "Enable Script" to true and enter the javascript in the "Script" box.
  4. 4.
    Press Save changes and copy the Webhook URL (added just below the script box).
  5. 5.
    Go to https://nixstats.com -> Settings -> Notification Contacts -> Add (or Edit a contact) contact.
  6. 6.
    Paste the Rocket.Chat url you've copied in step 4.
Paste this javascript in the "Script" textarea on Rocket.Chat webhook settings
1
/* exported Script */
2
/* globals console, _, s */
3
4
/** Global Helpers
5
*
6
* console - A normal console instance
7
* _ - An underscore instance
8
* s - An underscore string instance
9
*/
10
11
class Script {
12
/**
13
* @params {object} request
14
*/
15
process_incoming_request({ request }) {
16
var url;
17
var url_title;
18
if(request.content.domain_id)
19
{
20
url = 'https://nixstats.com/domains/'+request.content.domain_id;
21
url_title = request.content.name+" @ nixstats.com";
22
}
23
else
24
{
25
url = 'https://nixstats.com/server/'+request.content.server_id;
26
url_title = request.content.server_name+" @ nixstats.com";
27
}
28
return {
29
content:{
30
text: request.content.subject,
31
"attachments": [
32
{
33
"author_name": url_title,
34
"author_link": url,
35
"author_icon": "https://nixstats.com/images/favicon.png"
36
}
37
]
38
}
39
};
40
}
41
}
Copied!

Render nixstats graphs in Rocket.Chat

Add Nixstats notifications via a new WebHook in Rocket.Chat
  1. 1.
    In Rocket.Chat go to "Administration"->"Integrations" and create "New Integration".
  2. 2.
    Choose Outgoing WebHook.
  3. 3.
    Select Message Sent as Event trigger.
  4. 4.
    Enter ns as trigger word.
  5. 5.
    Enter <https://api.eu.nixstats.com/v1/> as URLs.
  6. 6.
    Avatar URL <https://nixstats.com/images/favicon.png>.
  7. 7.
    Token, this is your nixstats API token, create an API key.
  8. 8.
    Script Enabled set to True.
Paste this javascript in the "Script" textarea on Rocket.Chat webhook settings
1
/* exported Script */
2
/* globals Store */
3
4
class Script {
5
prepare_outgoing_request({ request }) {
6
let match;
7
8
match = request.data.text.match(/^ns servers\s(ls|list)\s*(.*)?$/);
9
if (match) {
10
let u = request.url + 'servers?perpage=99&token='+request.data.token;
11
return {
12
url: u,
13
headers: request.headers,
14
method: 'GET'
15
};
16
}
17
18
match = request.data.text.match(/^ns graphs\s(.*)?$/);
19
if (match) {
20
var matched = false;
21
var options;
22
var serverrequest = HTTP('GET', request.url + 'servers?perpage=99&token='+request.data.token, options);
23
var serverlist = []
24
JSON.parse(serverrequest.result.content).servers.forEach(function(pr) {
25
serverlist.push({'name': pr.name, 'id': pr.id});
26
});
27
28
serverlist.forEach(function(serv) {
29
if(serv.id == match[1])
30
{
31
matched = serv.id;
32
}
33
if(serv.name == match[1])
34
{
35
matched = serv.id;
36
}
37
});
38
39
if(!matched){
40
return {
41
message: {
42
text: 'Server not found.'
43
}
44
};
45
}
46
else
47
{
48
let u = request.url + 'server/'+matched+'?charts=yes&token='+request.data.token;
49
return {
50
url: u,
51
headers: request.headers,
52
method: 'GET'
53
};
54
}
55
}
56
57
match = request.data.text.match(/^help$/);
58
if (match) {
59
return {
60
message: {
61
text: [
62
'**Nixstats commands**',
63
'```',
64
' ns servers ls|list',
65
' ns graphs serverid|servername',
66
'```'
67
].join('\n')
68
}
69
};
70
}
71
}
72
73
process_outgoing_response({ request, response }) {
74
var text = [];
75
var attach = [];
76
if(response.content.charts)
77
{
78
response.content.charts.forEach(function(pr) {
79
attach.push({
80
"color": "#000000",
81
"text": pr.title+" on "+response.content.name,
82
"image_url": pr.url,
83
});
84
});
85
text.push('Performance of '+response.content.name);
86
}
87
else
88
{
89
text.push('```');
90
response.content.servers.forEach(function(pr) {
91
text.push(''+pr.id+"\t "+pr.last_data.load.replace(",",",\t")+"\t"+pr.name+'');
92
});
93
text.push('```');
94
}
95
return {
96
content: {
97
text: text.join('\n'),
98
attachments: attach,
99
parseUrls: false
100
}
101
};
102
}
103
}
Copied!
After saving the data you can use the following commands to retrieve data.
ns servers list to list your servers with their IDs and load average.
ns graphs [serverid] to retrieve a graph of Memory, Network, Load average and Disk usage of the specified server.
Export as PDF
Copy link