You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

In this section, there is a full example of how to automate report creation with email delivery using License Statistics API and common script languages for Windows and Linux.

Scenario

Let's assume we want to create a Usage Per User report and deliver it to the e-mail address in PDF format. Some additional report options are included:

ParameterValue
API Endpoint/api/v3/license-server/(server_id)/usage-per-user/(output_format)
Date RangeCurrent Year
License Server Id15
Aggregation TypeUser and Host
GranularityDay
Offset 0
Limit20
Order

descending by Date

Scripts

There are two example scripts attached:

report.shreport.ps1


Each has a couple of section that needs to be adjusted:

SectionPropertyDescriptionPowerShell example (report.ps1)Bash example (report.sh)
Configuration



License Statisticslicstat_hostURL to the License Statistics server$licstat_host = "https://licstat.example.com"licstat_host=https://licstat.example.com
api_tokenAPI token to authorize requests$api_token = "63675f08c441516f3ac4370deb28e01b"api_token=63675f08c441516f3ac4370deb28e01b

Date Range

You can also use custom values one of the pre-defined values in the script

  • CURRENT YEAR
  • LAST YEAR
  • CURRENT QUARTER
  • LAST QUARTER
  • CURRENT MONTH
  • LAST MONTH
  • CURRENT WEEK
  • LAST WEEK
  • YESTERDAY


start_dateStart date of the report $start_date = Get-Date -Format "yyyy-01-01"start_date=$(date +%Y-01-01)
end_dateEnd date of the report$end_date = Get-Date -Format "yyyy-MM-dd"end_date=$(date --iso-8601=date)

Report

All properties depend on specific reports and their requirements, it's just an example to cover the scenario. Adust the parameters and their values to your needs.

server_id

License Server Id

$server_id = 15server_id=15
aggregation_typeUSERNAME, HOSTNAME, USER_HOST, USERGROUP, HOSTGROUP$aggregation_type = "USER_HOST"aggregation_type=USER_HOST
granularityHOUR, DAY, WEEK, MONTH, QUARTER, YEAR$granularity = "DAY"granularity=DAY
offsetHow many rows to skip from the beginning$offset = 0offset=0
limitMaximum number of rows to return$limit = 20limit=20
orderString representation of column ordering$order = '[{"property":"fud","direction":"DESC"}]'order='[{"property":"fud","direction":"DESC"}]'
fields_visibilityList of fields to be included$fields_visibility = ('{"fud":1,"ugn":0,"hgn":0,"un":1,"uid":0,"hn":1,"hid":0,' +
    '"hip":0,"lsn":1,"lsid":0,"lsd":0,"fns":1,"fv":1,"ftype":0,"fcol":0,"fid":0' +
    ',"fd":0,"musage":1,"hu":1,"minu":0,"au":0,"mu":1,"hb":0,"minb":0,"mb":0,"ldtc":1,"ft":1}')
fields_visibility='{"fud":1,"ugn":0,"hgn":0,"un":1,"uid":0,"hn":1,"hid":0,"hip":0,
"lsn":1,"lsid":0,"lsd":0,"fns":1,"fv":1,"ftype":0,"fcol":0,"fid":0,"fd":0,
"musage":1,"hu":1,"minu":0,"au":0,"mu":1,"hb":0,"minb":0,"mb":0,"ldtc":1,"ft":1}'
Outputoutput_formatJSON, CSV, XLSX, PDF$output_format = "PDF"output_format=PDF
output_filenameFilename to save the report to$output_filename = "report"output_filename=report
Download Report    report_fileReport filename format$report_file = "$($output_filename).$($output_format.ToLower())"report_file="${output_filename}.$(echo ${output_format} | tr '[:upper:]' '[:lower:]')"
request_parametersList of request parameters including specific API endpoint and its requirements curl \
    --silent \
    --fail \
    -X POST \
    --location "${licstat_host}/api/v3/license-server/${server_id}/usage-per-user/${output_format}" \
    --header "X-Auth-token: ${api_token}" \
    --data-urlencode "lsid=${server_id}" \
    --data-urlencode "sd=${start_date}" \
    --data-urlencode "ed=${end_date}" \
    --data-urlencode "grat=${granularity}" \
    --data-urlencode "agrt=${aggregation_type}" \
    --data-urlencode "offset=${offset}" \
    --data-urlencode "limit=${limit}" \
    --data-urlencode "order=${order}" \
    --data-urlencode "fieldsVisibility=${fields_visibility}" \
    --output "${report_file}" || exit 1
$request_parameters = @{
    Uri = "$($licstat_host)/api/v3/license-server/$($server_id)/usage-per-user/$($output_format)"
    Headers = @{"X-Auth-token" = $api_token}
    OutFile = $report_file
    Method = "POST"
    Body = @{
        lsid = $server_id
        sd = $start_date
        ed = $end_date
        grat = $granularity
        agrt = $aggregation_type
        offset = $offset
        limit = $limit
        order = $order
        fieldsVisibility = $fields_visibility
    }
}
Send Report by email subjectEmail subject

recipientRecipient email address

bodyEmail body

fromSender email address

smtp_serverSMTP server address

Executing

PowerShell

Run the script from the console

PS> .\report.ps1


Run the script from the command line

powershell.exe -File report.ps1

Bash

$ ./report.sh

Execute periodically

Use can execute the script periodically using Windows Task Scheduler or Linux CRON.



  • No labels