-
Notifications
You must be signed in to change notification settings - Fork 0
/
cisco.js
140 lines (86 loc) · 3.33 KB
/
cisco.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var http = require("http");
var request = require("request");
const HOST = 'localhost';
const PORT = 7000;
// Twitter package
var Twitter = require('twitter');
// add code to read and set any environment variables with the dotenv package
require("dotenv").config();
// link keys
var keys = require("./keys.js");
var client = new Twitter(keys.twitter);
// -----------------------------------------------------
function networkCheck() {
var token;
// Token options
var optionsToken = {
method: 'POST',
url: 'https://10.10.20.85/api/system/v1/auth/token',
rejectUnauthorized: false,
headers:
{ Authorization: 'Basic YWRtaW46Q2lzY28xMjM0IQ==' }
};
request(optionsToken, function (error, response, body) {
if (error) throw new Error(error);
var body = JSON.parse(body)
token = body.Token
// console.log(token); // Sanity Check
// Device Check options
var optionsDeviceCheck = {
method: 'GET',
url: 'https://10.10.20.85/api/v1/network-device',
rejectUnauthorized: false,
headers:
{
'X-Auth-Token': token
}
};
request(optionsDeviceCheck, function (error, response, body) {
if (error) throw new Error(error);
var body = JSON.parse(body)
var date = Date()
var errors = 0
// var statusStr = `Date: ${date}\nThe Network is Up\nThere are ${errors} error(s).`
console.log(date);
body.response.forEach(dev => {
if (dev.errorDescription !== null) {
var status = `Date: + ${date}\nDevice: ${dev.type}\nError: ${dev.errorDescription}`; // dev.type should be dev.hostname - having issues.
errors ++
tweetStatus("Alert!\n" + status)
} else {
// console.log(status)
}
});
var statusStr = `Date: ${date}\nThe Network is Up\nThere are ${errors} error(s).`
// console.log(statusStr);
tweetStatus(statusStr);
});
});
}
// -----------------------------------------------------
/*(1) Use a twitter account to 'tweet' an hourly network status report. The tweet should be brief and convey only the key points, for example:
Date/Time: The network is up! No issue detected
Date/Time: The network is up! (2) issues detected within the past hour
*/
function tweetStatus(status) {
client.post('statuses/update', {status: status}, function(error, tweet, response) {
if(error) throw error;
console.log(`${tweet.text}`); // Tweet body.
// console.log(response); // Raw response object.
});;
}
// Hourly Timeout to check status
function hourlyNetworkCheck() {
networkCheck()
}
setInterval(hourlyNetworkCheck, 60 * 60 * 1000); // 1 Hour total
// setInterval(hourlyNetworkCheck, 5 * 60 * 1000); // 5min total
// -----------------------------------------------------
networkCheck()
// Server Stuff
const server = http.createServer((req, res) => {
res.end('Monitoring Network');
});
server.listen(PORT, HOST, () => {
console.log(`Server running at http://${HOST}:${PORT}/`);
});