Examples

mallocdelta

#!/usr/bin/env python3

"""
Script to collect and monitor the memory usage of a zone.

Example:
    mallocdelta.py hostname username password zonename interval(in sec)
"""

###############################################################################
#
#                          NO WARRANTY
#
#  BECAUSE THE PROGRAM IS PROVIDED FREE OF CHARGE, THERE IS NO WARRANTY
#  FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
#  OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
#  PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
#  OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
#  TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
#  PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
#  REPAIR OR CORRECTION.
#
#  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
#  WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
#  REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
#  INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
#  OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
#  TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
#  YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
#  PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGES.
#
###############################################################################

import activelogic
from activelogic import SysDiagMallocUsageZone
import sys
import time


class MallocDelta:
    def __init__(self, sd, name):
        self.sd = sd

        zones = self.sd.list(SysDiagMallocUsageZone)
        self.zone = next(iter([z for z in zones if z.zonename == name]), None)

        if not self.zone:
            print("Available zones: %s" % [z.zonename for z in zones])
            sys.exit(1)

        self.meminfo = sd.malloc_usage_get(self.zone)
        self.starttime = 0

    def dump_time(self):
        print(time.ctime())

    def dump(self):
        self.dump_time()
        self.starttime = time.time()
        for x in sorted(self.meminfo, key=lambda a: a.name):
            print("TOTAL[%.0f]: %s %d bytes (%d entries). Total count: %d" % (
                self.starttime, x.name, x.cur_bytes, x.tot_allocs, x.cur_allocs))

    def dump_delta(self):
        self.dump_time()
        timedelta = time.time() - self.starttime
        prev = {}
        for x in sorted(self.meminfo, key=lambda a: a.name):
            prev[x.name] = x.cur_bytes, x.cur_allocs

        self.meminfo = self.sd.malloc_usage_get(self.zone)

        for x in sorted(self.meminfo, key=lambda a: a.name):
            newval = x.cur_bytes, x.cur_allocs
            prevval = prev.get(x.name, (0, 0))
            if newval != prevval:
                print("DELTA[+%.0f]: %s %+d bytes (%+d entries). Now total: %d" % (
                    timedelta, x.name, newval[0] - prevval[0], newval[1] - prevval[1], newval[0]))


if len(sys.argv) != 6:
    print("Usage: mallocdelta.py host user password zone interval(in sec)")
    print("  e.g: mallocdelta.py 127.0.0.1 admin password LiveView 10")
    sys.exit(1)

al = activelogic.sync_connect(*sys.argv[1:4])
sd = al.sysdiag()
md = MallocDelta(sd, sys.argv[4])

md.dump()

while True:
    time.sleep(int(sys.argv[5]))
    md.dump_delta()

sysdiagdump

#!/usr/bin/env python3

"""
Script to collect and dump the aggregated (aka top-level) values of all System Diagnostics Zone
into a given file with json format.

Example:
    sysdiagdump.py hostname username password jsonfilename
"""

###############################################################################
#
#                          NO WARRANTY
#
#  BECAUSE THE PROGRAM IS PROVIDED FREE OF CHARGE, THERE IS NO WARRANTY
#  FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
#  OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
#  PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
#  OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
#  TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
#  PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
#  REPAIR OR CORRECTION.
#
#  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
#  WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
#  REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
#  INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
#  OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
#  TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
#  YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
#  PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGES.
#
###############################################################################

import asyncio
import sys
import json

from activelogic import async_connect, SysDiagZone


async def get_system_diagnostics_data(sd):
    """ Gets the zones and all the aggregated values from zones
    returns a dictionary of aggregated values in json format
    """
    zones = await sd.list(SysDiagZone)
    data = {}
    for zone in zones:
        zonedata = {}
        for aggrdata in await sd.get_aggregated_values(zone.oid):
            zonedata.update({zone.valuedefs[aggrdata.id].name: {
                "value": aggrdata.value,
                "rate": aggrdata.rate
            }})
        data.update({zone.name: zonedata})
    return data


async def amain(*args):
    """ Gets the zones and all the aggregated values from zones
    returns a dictionary of aggregated values in json format
    """
    host, username, password, dumpfile = args
    async with await async_connect(host, username, password) as al:
        async with await al.sysdiag() as sd:
            with open(dumpfile, 'w') as outputfile:
                json.dump(await get_system_diagnostics_data(sd), outputfile, indent=4)


if len(sys.argv) != 5:
    print("Usage: sysdiagdump.py hostname username password jsonfilename")
    print("  e.g: sysdiagdump.py localhost admin password sysdiagdump.json")
    sys.exit(1)

asyncio.run(amain(*sys.argv[1:5]))