-
Notifications
You must be signed in to change notification settings - Fork 18
/
LogstashPlugin.groovy
57 lines (49 loc) · 1.76 KB
/
LogstashPlugin.groovy
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
import com.dtolabs.rundeck.plugins.logging.StreamingLogWriterPlugin;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.dtolabs.rundeck.core.logging.LogEvent;
import com.dtolabs.rundeck.core.logging.LogLevel;
/**
* Opens a TCP connection, and writes JSON event messages to the socket
*/
rundeckPlugin(StreamingLogWriterPlugin){
configuration{
host defaultValue:"localhost", required:true, description: "Hostname to connect to"
port required:true, description: "Port to connect to", type: 'Integer'
}
/**
* open the socket, prepare some metadata
*/
open { Map execution, Map config ->
def socket = new Socket(config.host, config.port.toInteger());
def socketStream = socket.getOutputStream();
def e2 = [:]
execution.each{ e2["execution.${it.key}"]=it.value }
def json=new ObjectMapper()
[socket:socket, count:0, executionid:execution.execid, write:{
socketStream<< json.writeValueAsString(e2 + it) + "\n"
}]
}
/**
* write the log event and metadata as json to the socket
*/
addEvent { Map context, LogEvent event->
context.count++
def emeta=[:]
event.metadata?.each{ emeta["event.${it.key}"]=it.value }
def data= emeta + [
line:context.count,
datetime:event.datetime.time,
loglevel:event.loglevel.toString(),
message:event.message,
eventType:event.eventType,
]
context.write data
}
/**
* close the socket
*/
close {
context.write ending:true, totallines:context.count, message: 'Execution '+context.executionid+' finished.'
context.socket.close();
}
}