1

There is requirement to collect only specific stats like %idle, memfree, available, pmemused etc. I further have to convert the output to a json string and consume it.

Is there any UNIX tool which can do the same?

I tried sar and sadf in combination, but I am getting a complex json format which I don't require.

/usr/bin/sar -r 1 1 -o tmp1 &> 1;
/usr/bin/sadf tmp1 -j --iface=eth1 -- -u -r -n DEV

I get this output:

{
    "sysstat": {
        "hosts": [{
            "nodename": "ESDNAS1",
            "sysname": "Linux",
            "release": "4.4.143-94.47-default",
            "machine": "x86_64",
            "number-of-cpus": 8,
            "file-date": "2019-08-02",
            "file-utc-time": "04:53:09",
            "statistics": [{
                "timestamp": {
                    "date": "2019-08-02",
                    "time": "04:53:10",
                    "utc": 1,
                    "interval": 1
                },
                "cpu-load": [{
                    "cpu": "all",
                    "user": 0.25,
                    "nice": 0.00,
                    "system": 1.00,
                    "iowait": 0.00,
                    "steal": 0.00,
                    "idle": 98.75
                }],
                "memory": {
                    "memfree": 3707764,
                    "avail": 12451860,
                    "memused": 12013448,
                    "memused-percent": 45.01,
                    "buffers": 225176,
                    "cached": 4361204,
                    "commit": 28665304,
                    "commit-percent": 107.41,
                    "active": 13473076,
                    "inactive": 1535816,
                    "dirty": 616
                },
                "network": {
                    "net-dev": [{
                        "iface": "eth1",
                        "rxpck": 1.00,
                        "txpck": 1.00,
                        "rxkB": 0.11,
                        "txkB": 0.00,
                        "rxcmp": 0.00,
                        "txcmp": 0.00,
                        "rxmcst": 0.00,
                        "ifutil-percent": 0.00
                    }]
                }
            }],
            "restarts": []
        }]
    }
}

I am looking for something like this (or close one which is simple):

{
  "sysstat": {
    "hosts": [
      {
        "nodename": "HOSTNAME",
        "statistics": [
          {
            "timestamp": {
              "date": "2019-08-02",
              "time": "04:53:10"
            },
            "cpu-load": [
              {
                "idle": 98.75
              }
            ],
            "memory": {
              "memfree": 3707764,
              "avail": 12451860,
              "memused-percent": 45.01
            },
            "network": {
              "net-dev": [
                {
                  "iface": "eth1",
                  "rxpck": 1,
                  "txpck": 1,
                  "ifutil-percent": 0
                }
              ]
            }
          }
        ],
        "restarts": []
      }
    ]
  }
}
1

I think jq is the tool of choice.

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

A brilliant tutorial on jq can be found at Github https://stedolan.github.io/jq/tutorial/ and a nice one here: https://programminghistorian.org/en/lessons/json-and-jq.

For example, to get to the "memfree" part, pipe the output of your commands to jq like this:

... | jq .[].hosts[].statistics[].memory.memfree

which gives:

3707764

Or, to get the whole memory part in a JSON:

... | | jq .[].hosts[].statistics[].memory

which gives:

{
  "memfree": 3707764,
  "avail": 12451860,
  "memused": 12013448,
  "memused-percent": 45.01,
  "buffers": 225176,
  "cached": 4361204,
  "commit": 28665304,
  "commit-percent": 107.41,
  "active": 13473076,
  "inactive": 1535816,
  "dirty": 616
}
|improve this answer|||||
3

You can use psutil in python and create your customized json.

psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful mainly for system monitoring, profiling, limiting process resources and the management of running processes. It implements many functionalities offered by UNIX command line tools such as: ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap.

Example:

import json
import psutil
def used_mem(json_key):
        mem_total = psutil.virtual_memory().total
        mem_percent = psutil.virtual_memory().percent
        mem_used = psutil.virtual_memory().used
        mem_free = psutil.virtual_memory().free
        swap_percent =  psutil.swap_memory().percent
        swap_total =  psutil.swap_memory().total
        swap_used =  psutil.swap_memory().used
        json_key['free_mem'] = mem_free
        json_key['total_mem'] = mem_total
        json_key['mem_used'] = mem_used
        json_key['percent_mem'] = mem_percent
        json_key['swap_percent'] =  swap_percent
        json_key['swap_total'] =  swap_total
        json_key['swap_used'] =  swap_used

json_key={}
used_mem(json_key)
print json.dumps(json_key)

output:

{"swap_used": 14061568, "percent_mem": 48.3, "free_mem": 1792401408, "total_mem": 12447776768, "mem_used": 5128196096, "swap_total": 11999899648, "swap_percent": 0.1}

useful link: https://github.com/giampaolo/psutil

|improve this answer|||||
1

Do this with JSON parser jq as below if you are not able to tweak the original command (sar or sadf) to produce the JSON as you expect

jq '.sysstat.hosts[] |= { nodename, statistics : [ { timestamp : .statistics[].timestamp | { date, time }, 
    "cpu-load": .statistics[]."cpu-load"[] | [{ idle }], 
    "memory": .statistics[].memory | { memfree, avail, "memused-percent" }, 
    "network" : { "net-dev" : .statistics[].network."net-dev"[] | { iface, rxpck, txpck, "ifutil-percent"} } } ] , restarts}'

You can see this filter working in jq-playground on jq-1.6

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.