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

Axeda Groovy Script: List of User Logins/Logouts from Audit

Highlighted
Garnet

Axeda Groovy Script: List of User Logins/Logouts from Audit

This groovy script creates an xml output of the audit log filtered by the User Access category, so dates of when users logged in or logged out.

Parameter:

days - number of days to search

import com.axeda.drm.sdk.device.ModelFinder

import com.axeda.drm.sdk.Context

import com.axeda.common.sdk.id.Identifier

import com.axeda.drm.sdk.device.Model

import com.axeda.drm.sdk.device.DeviceFinder

import com.axeda.drm.sdk.device.Device

import com.axeda.drm.sdk.audit.AuditCategoryList

import com.axeda.drm.sdk.audit.AuditCategory

import com.axeda.drm.sdk.audit.AuditEntryFinder

import com.axeda.drm.sdk.audit.SortType

import com.axeda.drm.sdk.audit.AuditEntry

import groovy.xml.MarkupBuilder

/*

* AuditEntryList.groovy

*

* Creates an xml output of the audit log filtered by the User Access category, so dates of when users logged in or logged out.

*

* @param days        -   (REQ):Str number of days to search.

*

* @author Sara Streeter <sstreeter@axeda.com>

*/

def writer = new StringWriter()

def xml = new MarkupBuilder(writer)

try {

    def ctx = Context.getUserContext()

    ModelFinder modelFinder = new ModelFinder(ctx, new Identifier(1))

    Model model = modelFinder.find()

    DeviceFinder deviceFinder = new DeviceFinder(ctx, new Identifier(1))

    Device device = deviceFinder.find()

    AuditCategoryList acl = new AuditCategoryList()

    acl.add(AuditCategory.USER_ACCESS)

    long now = System.currentTimeMillis()

    Date today = new Date(now)

    def paramdays = parameters.days ? parameters.days: 5

    long days = 1000 * 60 * 60 * 24 * Integer.valueOf(paramdays)

    AuditEntryFinder aef = new AuditEntryFinder(ctx)

    aef.setCategories(acl)

    aef.setToDate(today)

    aef.setFromDate(new Date(now - (days)))

    aef.setSortType(SortType.DATE)

    aef.sortDescending()

    List<AuditEntry> audits = aef.findAll()

// assemble the response

    xml.Response() {

        audits.each { AuditEntry audit ->

     

            Audit() {

                id(audit?.id.value)

                user(audit?.user?.username)

                date(audit?.date)

                category(audit?.category?.bundleKey)

                message(audit?.message)

            }

        }

    }

} catch (def ex) {

    xml.Response() {

        Fault {

            Code('Groovy Exception')

            Message(ex.getMessage())

            StringWriter sw = new StringWriter();

            PrintWriter pw = new PrintWriter(sw);

            ex.printStackTrace(pw);

            Detail(sw.toString())

        }

    }

}

return ['Content-Type': 'text/xml', 'Content': writer.toString()]