-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.js
executable file
·158 lines (118 loc) · 3.65 KB
/
sample.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env node
var http = require('http')
var util = require('util')
var __assert = require('assert')
var debug = require('debug')('es-tailf')
var pad0 = require('./util').pad0
var argv = require('optimist')
.usage()
.default({
'es-port': 9200,
'source-host': '*',
'f': ['@timestamp', '@source_host', '@source_path', '@message']
})
.describe('f', 'fields to display (see http://kibana.tst.ape.yandex.net for more info)')
.describe('source-path', '@source_path query')
.describe('source-host', '@source_host query')
.demand(['source-path', 'es-host'])
.argv
var HOST = argv['source-host']
var SOURCE_PATH = argv['source-path']
var FIELDS = argv.f
var INTERVAL = 60
var ES_HOST = argv['es-host']
var ES_PORT = argv['es-port']
var d = new Date()
var ES_INDEX = util.format('logstash-%d.%s.%s', d.getFullYear(), pad0(d.getMonth()+1, 2), pad0(d.getDate(), 2))
function getQuery(needle, options){
options = options || {}
return {
fields: options.fields || ['@message'],
query:
{ filtered:
{ query: { bool: { should: [ { query_string: { query: '*' } } ] } },
filter:
{ bool:
{ must:
[ { range: { '@timestamp': { from: Date.now()-INTERVAL*1000, to: Date.now() } } },
{ fquery:
{ query: { query_string: { query: '@source_path:('+needle+')' } },
_cache: true } },
{ fquery:
{ query: { query_string: { query: '@source_host:('+options.host+')'} },
_cache: true } }] } } } },
size: 5000,
sort: [ { '@timestamp': { order: 'asc', ignore_unmapped: true } } ] }
}
function doQuery(){
var fields = FIELDS
var query = getQuery(SOURCE_PATH,
{fields:fields,
host:HOST})
var queryData = JSON.stringify(query)
var post_options = {
host: ES_HOST,
port: ES_PORT,
path: util.format('/%s/_search',ES_INDEX),
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': queryData.length
}
}
debug('query', util.inspect(query, {depth: null}))
debug('post_options', util.inspect(post_options))
var postReq = http.request(post_options, function(res) {
var chunks = []
res.setEncoding('utf8')
res.on('data', function (chunk) {
//console.log('================ chunk length',chunk.length)
chunks.push(chunk)
})
res.on('end', function(){
querying = false
//console.log('done')
var body = chunks.join('')
var result = JSON.parse(body)
debug('result', util.inspect(result, {depth:null}))
var hits = result.hits.hits
processResultHits(hits, fields)
scheduleQuery()
})
})
postReq.end(queryData)
postReq.on('error', function(){
console.error('request error', arguments)
querying = false
process.nextTick(scheduleQuery)
})
}
var entries = {}
function processResultHits(hits, fields){
var updates = []
hits.some(function(hit){
if(!(hit._id in entries)){
entries[hit._id] = hit
updates.push(hit)
}
})
if(0 < updates.length){
//console.log('================ updates')
updates.some(function(hit){
//console.log(util.inspect(hit, {depth:null}))
var entry = fields.map(function(f){
return hit.fields[f][0]
}).join(' ')
console.log('-'+(Date.now()-hit.sort[0])+'ms\t', entry)
//console.log(hit._id, hit.sort[0], '(emitted '+(Date.now()-hit.sort[0])+'ms earlier)', entry)
})
}
}
var querying = false
var queryTimeout = 100
function scheduleQuery(){
if(!querying){
setTimeout(doQuery, queryTimeout)
}
}
doQuery()