Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Custom scriptrunner REST endpoint for server logs

Rob B
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 24, 2019

Hi,

I know there is a built in script to view server logs, but i would like to create a custom REST endpoint so non admin users can access them via the url.

Is this possible?

Thanks

Rob

2 answers

1 accepted

1 vote
Answer accepted
Antoine Berry
Community Champion
April 24, 2019

Hi @Rob B ,

I guess this would be possible since you can create custom REST endpoints in groovy with scriptrunner. From there you would need to read from the log file (quick search on stackoverflow).

Antoine

Rob B
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 24, 2019

Hi Antonie, Im lookin to see if there is code someone else has used so i can just change to pick the logs i need. Im not experienced with groovy so im a bit stuck.

Antoine Berry
Community Champion
April 24, 2019

I do not think this question has been asked before. I'll check what I can do.

Like Rob B likes this
Antoine Berry
Community Champion
April 24, 2019

So I have figured out to return the whole log file, you need to add a custom rest end point with this code (change name to your liking) : 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response


@BaseScript CustomEndpointDelegate delegate

getLogs(httpMethod: "GET", groups: ["jira-users"]) { MultivaluedMap queryParams, String body ->
String fileContents = new File('/app/list/data/installedApps/jira/log/atlassian-jira.log').text
return Response.ok(new JsonBuilder([logs: fileContents]).toString()).build()
}

 Of course update the file directory with yours. I have tried my best to come up with a script that returns the n last lines, but it seems to be pretty difficult with groovy, so I hope your log files are restrained to acceptable sizes.

You can then call the rest service with 

<base-url>/rest/scriptrunner/latest/custom/getLogs

Let me know how that went.

Antoine

Like Sana Safai likes this
0 votes
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 24, 2019

You can just call an instance of the LogFileViewer class in your REST API after setting the authentication context to an admin that has permission to run such scripts.

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.common.admin.LogFileViewer

def adminUser = ComponentAccessor.userManager.getUserByName('admin')

ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(adminUser)

def logViewer = new LogFileViewer()
def inputs = [
(LogFileViewer.FIELD_LOG_FILE) : "atlassian-jira.log",
(LogFileViewer.FIELD_LOG_FILE_HMTL) : "",
(LogFileViewer.FIELD_N_LINES) : "100"
]
def result = logViewer.doScript(inputs)

return result.output

This can methodology can be used for any of the built-in scripts.

I do that to allow non-admin users to create new projects based on a specific template project (hard coded in my resp api endpoint).

Antoine Berry
Community Champion
April 25, 2019

Hi @PD Sheehan ,

This looks cool! Where are you using this script ?

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 25, 2019

Do you mean the LogFileViewer?
I just put this together on the fly in my ScriptRunner console by borrowing from other scripts. 

But it would be easy to embed into a custom rest endpoint like you suggested and have the logfile and line number limit (perhaps with a hardcoded max) be passed as query parameters.

This is what I did with the the CopyProject built-in script. I embeded it in a REST API an I can control via Jira Group who has access to that endpoint.

Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer