API

Arjen van Bochoven edited this page Apr 12, 2017 · 6 revisions
Clone this wiki locally

To get data out of Munkireport there is a Datatables API that is also used for the listing reports. To help you get started use the small shell script or python script below. To get the data to CSV format, pipe the output to JSON to CSV parser (see below).

Shell Version

#!/bin/sh
#
# Script to run automated queries against the munkireport datatables API
#
# Results are returned in JSON format
# The actual entries are in the 'data' variable
#
# To make this work, set up a regular user in munkireport and adjust the 
# proper values below
#
# Author: Arjen van Bochoven
# Date: 2015-11-06

# Retrieve data from munkireport
# DEBUG=1
MR_BASE_URL='https://yourmunkireportserver/index.php?'
MR_DATA_QUERY='/datatables/data'
MR_LOGIN='datauser'
MR_PASSWORD='secretpassword'

CLIENT_COLUMNS=(
    "machine.serial_number"
    "machine.hostname"
    "machine.machine_desc"
    "reportdata.timestamp"
    "reportdata.console_user"
    "machine.os_version"
    "reportdata.remote_ip"
    "munkireport.manifestname"
)

# Create query from columns
columns_to_query()
{
    # Pick up array as argument
    declare -a COLUMNS=("${!1}")
    
    MR_QUERY=""
    COL=0
    for i in "${COLUMNS[@]}"; do
        MR_QUERY="${MR_QUERY}columns[${COL}][name]=${i}&"
        COL=$((COL+1))
    done
}

# Authenticate and capture cookie
if [ $DEBUG ]; then echo 'Authenticating to munkireport..'; fi
COOKIE_JAR=$(curl -s --cookie-jar - --data "login=${MR_LOGIN}&password=${MR_PASSWORD}" ${MR_BASE_URL}/auth/login)
SESSION_COOKIE=$(echo $COOKIE_JAR | sed 's/.*PHPSESSID /PHPSESSID=/')

# Retrieve data with session cookie
columns_to_query CLIENT_COLUMNS[@]
if [ $DEBUG ]; then echo 'Retrieving client data..'; fi
echo $(curl -s --cookie "$SESSION_COOKIE" --data $MR_QUERY ${MR_BASE_URL}${MR_DATA_QUERY})

Python Version using Requests

#!/usr/bin/python
import requests

base_url='https://munkireport.example.com/index.php?'
login=''
password=''

columns=[
    "machine.serial_number",
    "machine.hostname",
    "machine.machine_desc",
    "reportdata.timestamp",
    "reportdata.console_user",
    "machine.os_version",
    "reportdata.remote_ip",
    "munkireport.manifestname"
]

# authenticate and get a session cookie
auth_url ='{0}/auth/login'.format(base_url)
query_url='{0}/datatables/data'.format(base_url)
session = requests.Session()
auth_request = session.post(auth_url, data={'login': login, 'password': password})

if auth_request.status_code != 200:
    print 'Invalid url!'
    raise SystemExit

def generate_query():
    q = {'columns[{0}][name]'.format(i): c for i, c in enumerate(columns)}
    return q

query_data = session.post(query_url, data=generate_query())
print query_data.json()

Convert to CSV

If you need the data in Comma Separated Value format (csv), you could pipe the results of the above script into this little python snippet:

#!/usr/bin/python
# Convert Munkireport Datatables json output to csv

import csv, json, sys

input = "".join(sys.stdin.readlines())
data = json.loads(input)['data']

class SKV(csv.excel):
    # like excel, but uses semicolons
    delimiter = ";"

csv.register_dialect("SKV", SKV)

output = csv.writer(sys.stdout, "SKV")

for row in data:
    output.writerow(row)