Forum Discussion

manthena2020's avatar
10 months ago

Eventsource

hi iam using te following code for event script. The output iam getting is There would be no script events for the selected device. IF iam running in datasource the script is working both in jason format

import groovy.sql.Sql
import groovy.json.JsonBuilder
import org.json.JSONObject

def hostname = hostProps.get("system.hostname")
def user = hostProps.get("sybase.user")
def pass = hostProps.get("sybase.pass")
def port = 21000

def url = "jdbc:sybase:Tds:" + hostname + ":21000"
def driver = "com.sybase.jdbc4.jdbc.SybDriver"

def sql = Sql.newInstance(url, user, pass, driver)

if (sql.connection) {
// println("Connected to Sybase server on ${hostname}")

def filePath = "/tmp/BTS01DEV_WNL_DS.log" // Replace with the actual file path on the Linux machine

def file = new File(filePath)
if (file.exists() && file.isFile()) {
def lines = file.readLines()

// Define the keywords to search for
def keywords = [
"warning",
"error",
"fail",
// Add more keywords as needed
]

// Find the lines that contain the keywords along with their indices
def matchedLines = lines
.findAll { line -> keywords.any { keyword -> line.toLowerCase().contains(keyword) } }
.collect { line -> [line: lines.indexOf(line) + 1, message: line] }

// Construct the final JSON object
def json = new JsonBuilder()
json {
events matchedLines
}

// Convert the JSON object to a string
def jsonString = json.toString()

// Remove leading characters until the first '{' character
def startIndex = jsonString.indexOf('{')
def trimmedJsonString = jsonString.substring(startIndex)

// Create the JSONObject from the trimmed JSON string
def jsonObject = new JSONObject(trimmedJsonString)

println(jsonObject.toString())
} else {
println("File not found or is not a regular file")
}
} else {
println("Failed to connect to Sybase server on ${hostname}")
}


out put in datasource:


{"events":[{"line":41,"message":"00:0000:00000:00000:2021/04/21 14:54:02.58 kernel Warning: Operating System stack size is greater than 2MB. If it is too large, ASE may run out of memory during thread creation. You can reconfigure it using 'limit' (csh) or 'ulimit' (bash)"},{"line":177,"message":"00:0006:00000:00002:2021/04/21 14:54:03.46 kernel Warning: Cannot set console to nonblocking mode, switching to blocking mode."},{"line":313,"message":"00:0000:00000:00000:2021/04/21 16:42:08.18 kernel Warning: Operating System stack size is greater than 2MB. If it is too large, ASE may run out of memory during thread creation. You can reconfigure it using 'limit' (csh) or 'ulimit' (bash)"},{"line":636,"message":"00:0002:00000:00002:2021/04/21 16:41:26.28 kernel Warning: Cannot set console to nonblocking mode, switching to blocking mode."}

5 Replies

  • You’re missing ]} at the end of your json. I don’t follow your logic for converting to json from the data, so I’m not sure how to help you fix that. If the eventsource doesn’t receive valid json, it won’t parse it and there would be no events.

  • Here I am trying to read log file , the log file over there is live log file it generates based on the events in form of lines for each event.

    As, LM want the script output in Jason format for event scripts, I am converting it to Jason.

  • As per the documentation

    Script Output Format

    Regardless of whether it is an embedded or external script, the output needs to be a JSON object with an array of one or more events, like this:

    {    "events" : [                  {                     "happenedOn":"Fri Jun 05 09:17:47 UTC 2015",                      "severity":"Warn",                      "message":"This is the first event's message",                      "Source":"This is the source of the first event"                   },                   {                     "happenedOn":"Fri Jun 07 09:27:37 UTC 2015",                      "severity":"critical",                      "message":"This is the second event's message",                      "Source":"This is the source of the second event"                   }               ]} 
  • Yeah, somewhere in your conversation you are not closing out your square and curly braces.