본문 바로가기

잡부생활

HTTP 헤더 모니터링

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
'use strict'
const AWS = require('aws-sdk')
AWS.config.region = 'ap-northeast-2'
const CronJob = require('cron').CronJob
const sns = new AWS.SNS();
const waterfall = require('async-waterfall')
const request = require('request')
const phoneNumber = ['+821020']
const conf = require('./conf.json')
const Slack = require('slack-node')
 
const webhookUri = "https://hooks.slack.com/services/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"
const slack = new Slack()
slack.setWebhook(webhookUri)
 
const sendSlack = (conf, checkCors, check) => {
        slack.webhook({
                "channel":"#tv",
                "username":"TV CORS Checker",
                "text""장애 발생\n"+"URI: "+conf.options.uri+"\nCORS: "+conf.cors+"\nCheck CORS: "+checkCors+"\nOrigin: "+conf.options.headers.origin
        }, (err, reponse) =>{
                if(err) console.log(err)
                else console.log(reponse)
        })
}
 
const req = (options, callback) => {
        request(
                options
                ,function(err, response, body){
                      if(err){
                                console.log('err:'+err);
                                callback(false);
                      }else{
                                const res_cors = response.headers['access-control-allow-origin']
//                              console.log('server encoded the data as: ' + (response.headers['age']))
//                              console.log('server encoded the data as: ' + (response.headers['access-control-allow-origin']))
//                              console.log('server encoded the data as: ' + (response.headers['server']))
                                callback(res_cors);
                      }
                 }
        )
}
const checker = (conf, corsCheck, callback) => {
        (conf.cors == corsCheck) ? callback(true) : callback(false)
}
const mail = (conf, checkCors, check) => {
        const host = conf.options.uri.split('/')
        let params = {
                Message: '장애 발생\n'+'URI: '+conf.options.uri+'\nCORS: '+conf.cors+'\nCheck CORS: '+checkCors+'\nOrigin:'+conf.options.headers.origin,
                Subject: '장애: '+host[2],
                TopicArn: 'arn:aws:sns:ap-northeast-2:557652101750:tv-apm'
        }
        sns.publish(params, function(err, data){
                if(err) console.log(err, err.stack)
                else    console.log(data)
        })
}
const send = (conf, phone, callback) => {
        const host = conf.options.uri.split('/')
        let params = {
                Message: '장애:'+host[2],
                MessageStructure: 'string',
                PhoneNumber: phone
        }
        sns.publish(params, function(err, data){
                if(err) console.log(err, err.stack)
                else    console.log(data)
                callback('장애:'+conf.options.headers.uri)
        })
}
const cron = () =>{
waterfall([
        function(callback){
                for(let i in conf){
                        req(conf[i].options, function(data){
//                              console.log(conf[i].options.uri)
                                callback(null, conf[i], data)
                        })
                }
        }, function(conf, checkCors, callback){
                checker(conf, checkCors, function(data){
                        callback(null, conf, checkCors, data)
                })
        }, function(conf, checkCors, check, callback){
                if(check == false){
                        console.log('장애 발생\n'+'URI: '+conf.options.uri+'\nCORS: '+conf.cors+'\nCheck CORS: '+checkCors+'\nOrigin: '+conf.options.headers.origin)
//                      send(conf[i], '+821062530420', function(data){
//                      })
                        mail(conf, checkCors, check)
                        sendSlack(conf, checkCors, check)
                }else{
                        console.log('정상\n'+'URI: '+conf.options.uri+'\nCORS: '+conf.cors+'\nCheck CORS: '+checkCors+'\nOrigin: '+conf.options.headers.origin)
                }
                callback('done')
        }
],function(err, result){
        if(err) console.log(err)
        else console.log(result)
})
}
const job = new CronJob({
  cronTime: '00 */3 * * * *',
  onTick: function() {
    // Runs every weekday (Monday through Friday)
    // at 04:30:00 AM. It does not run on Saturday
    // or Sunday.
    cron();
  },
  start: false,
  timeZone: 'Asia/Seoul'
});
job.start();
 
cs


프로세스 관리

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
'use strict'
const AWS = require('aws-sdk')
AWS.config.region = 'ap-northeast-2'
const CronJob = require('cron').CronJob
const sns = new AWS.SNS();
const waterfall = require('async-waterfall')
const request = require('request')
const phoneNumber = ['+821xxxxx420']
const conf = require('./conf.json')
const Slack = require('slack-node')
const exec = require('shelljs.exec')
var list = exec('pm2 list |grep reqCron', {silent: true})
const webhookUri = "https://hooks.slack.com/services/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"
const slack = new Slack()
slack.setWebhook(webhookUri)
const sendSlack = (check) => {
        slack.webhook({
                "channel":"#tv",
                "username":"TV CORS Checker",
                "text": check.stdout
        }, (err, reponse) =>{
                if(err) console.log(err)
                else console.log(reponse)
        })
}
const mail = (check) => {
        var params = {
                Message: check.stdout,
                Subject: 'TV 모니터링 프로세스',
                TopicArn: 'arn:aws:sns:ap-northeast-2:557652101750:tv-apm'
        }
        sns.publish(params, function(err, data){
                if(err) console.log(err, err.stack)
                else    console.log(data)
        })
}
const job = new CronJob({
  cronTime: '00 00 09,17 * * *',
  onTick: function() {
    // Runs every weekday (Monday through Friday)
    // at 04:30:00 AM. It does not run on Saturday
    // or Sunday.
        sendSlack(list)
//      mail(list)
  },
  start: false,
  timeZone: 'Asia/Seoul'
});
job.start();
 
cs


'잡부생활' 카테고리의 다른 글

OpenVPN, OpenLDAP  (0) 2019.04.23
Alibaba Cloud ECS 간단 사용기  (0) 2019.03.27
ffmpeg 활용 영상 모니터링  (0) 2017.09.05
Wowza 설정 관련  (0) 2017.07.04
AWS Windows2008 R2 Client NFS 설정  (0) 2017.03.22