コード例 #1
0
def return_servers_report(key: str, secret: str) -> str:
    """Returns a report of devices without anti-virus in CSV format.
    Keyword arguments:
    key:    str     -- NinjaRMM API Key ID
    secret: str     -- NinjaRMM API Secret ID
    """
    client = ninjarmmpy.Client(AccessKeyID=key, SecretAccessKey=secret)
    device_ids = client.getGroupDeviceIds(id=os.getenv(key='DEVICE_GROUP'))
    devices = [client.getDevice(id=i) for i in device_ids]
    output = []
    with StringIO() as f:
        fields = [
            'organization', 'dns_name', 'role', 'device_id', 'os_name',
            'needs_reboot', 'last_user', 'device_link'
        ]
        writer = DictWriter(f, fieldnames=fields)
        writer.writeheader()
        for d in devices:
            device_role = d['nodeClass']
            device_name = d['dnsName']
            device_org = client.getOrganization(id=d['organizationId'])['name']
            row = {
                'organization':
                device_org,
                'dns_name':
                device_name,
                'role':
                device_role,
                'device_id':
                d.get('id', None),
                'os_name':
                d.get('os', None).get('name', None),
                'needs_reboot':
                d.get('os', None).get('needsReboot', None),
                'last_user':
                d.get('lastLoggedInUser', None),
                'device_link':
                f"https://app.ninjarmm.com/#/deviceDashboard/{d.get('id', 'Error')}/overview"
            }
            output.append(row)
        writer.writerows(output)
        return f.getvalue()
コード例 #2
0
import ninjarmmpy
import os
import json

# Create our client
# Assuming we are storing our keys in environment variables we can access
client = ninjarmmpy.Client(AccessKeyID=os.environ.get('NRMM_KEY_ID'),
                           SecretAccessKey=os.environ.get('NRMM_SECRET'),
                           Europe=False)
# Get drives and controllers as Python dictionaries
drives = client.get_raid_drives()
controllers = client.get_raid_controllers()
# For this example, we're just going to convert the dictionaries to JSON and write them to a file.
drives = json.dumps(drives)
controllers = json.dumps(controllers)
# Now we can write the drives and raid controllers to JSON files.
with open('drives.json', 'w', newline='') as f:
    print(drives, file=f)

with open('controllers.json', 'w', newline='') as f:
    print(controllers, file=f)
コード例 #3
0
from flask import Flask, render_template
import ninjarmmpy

app = Flask(__name__)

# Create our client
# Assuming we are storing our keys in environment variables we can access
client = ninjarmmpy.Client(
    AccessKeyID='YourAccessKeyID',
    SecretAccessKey='YourSecretAccessKey',
    Europe=False
)

@app.route('/')
def home():

    #Call the API data and store it 
    devices_list = client.get_devices()
    org_list = client.get_organizations()
    controllers = client.get_raid_controllers()
    device_dets = client.get_devices_detailed()
    #setting this for conversion
    raid_info = controllers['results']
    #Containers for extracted data
    server_info = []
    server_offline_data = []
    #Loops through API data and stores it into above object
    for org_stuff in org_list:
        org_id = org_stuff['id']
        org_name = org_stuff['name']
        for device_data in devices_list: