Blog

Parse ohai output with jq

May 22, 2013 | Announcements, Migration, MSP

Ohai is a tool which is used  by chef-client to obtain information about  the node’s platform detail, networking usage, memory usage, processor usage , kernel data , host names, fully qualified domain names and other configuration. It also has command line tool which displays the data in Json.

ohai  | head -4

{

"languages": {

"ruby": {

"platform": "x86_64-darwin12.2.0",

As you can see it shows system information in json format. But it’s a challenge to extract this information from Json in command line. I found this cool tool  jq, you can download it from this site; it’s written in C and there is no runtime dependency.  It allows you parse Json easily, for example:

ohai -l error | jq '.ipaddress'

"192.168.1.8"

ohai -l error | jq '.uptime_seconds'

80222

Note: you should  use ohai -l error when you are piping the output to jq, because sometimes ohai throws warnings, and that will make json parsing fail because of unformatted json.

I find this very useful rather then trying to use awk to parse the IP address or any other node information you can easily use ohai and jq to get that information.

nClouds Insights

Join thousands of DevOps and cloud professionals. Sign up for our newsletter for updated informaion and insights

nClouds
nClouds is a cloud-native services company that helps organizations maximize site uptime, performance, stability, and support, bringing out the best of their people and technology using AWS