You are not logged in.

#1 2019-09-19 15:25:50

merlock
Member
Registered: 2018-10-30
Posts: 235

[SOLVED][conky + awk] Tighten up a script?

I found a little script that displays pi-hole stats on conky.  Wanted dual colors in the output, so I wrote a script that does that.  Took me a lot of reading and trial/error to get this.  The final output (pi3.txt) is what is imported into conky.

Looking for constructive criticism to tighten the script up.

2019-09-19-104404-254x147-scrot.png

#!/usr/bin/bash

curl pi.hole/admin/api.php | sed -e 's/.$//' -e 's/"//g' -e 's/enabled/Enabled/' > ~/temp/pi1.txt

awk -F '[:,]' '{print \
"Status -:"$32"\n" \
"Domains Blocked -:"$2"\n" \
"DNS Queries Today -:"$4"\n" \
"Queries Blocked -:"$6 " - (" int($8)"%)\n" \
"Queries Forwarded -:"$12"\n" \
"Queries Cached -:"$14"\n" \
"Unique Clients -:"$18"\n" \
"Last Update -:"$40"d "$42"h ago"'} ~/temp/pi1.txt > ~/temp/pi2.txt

awk -F: '{sub(/^/,"${color2}",$2)}''{sub(/^/,"${color}",$1); print}' ~/temp/pi2.txt > ~/temp/pi3.txt

exit 0

pi1.txt (Everything based on this output)

{domains_being_blocked:115967,dns_queries_today:16477,ads_blocked_today:1463,ads_percentage_today:8.879044,unique_domains:9175,queries_forwarded:9872,queries_cached:5137,clients_ever_seen:483,unique_clients:41,dns_queries_all_types:16477,reply_NODATA:1470,reply_NXDOMAIN:7940,reply_CNAME:3406,reply_IP:3433,privacy_level:0,status:Enabled,gravity_last_updated:{file_exists:true,absolute:1568531829,relative:{days:4,hours:07,minutes:44}}

Last edited by merlock (2019-09-20 02:07:26)


Eenie meenie, chili beanie, the spirits are about to speak -- Bullwinkle J. Moose
It's a big club...and you ain't in it -- George Carlin
Registered Linux user #149839
perl -e 'print$i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10); '

Offline

#2 2019-09-19 16:07:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,525
Website

Re: [SOLVED][conky + awk] Tighten up a script?

There's no need for temp files, just use pipes.  But then at that point there's no need for multiple awks and seds, they do the same thing.  Pick one, and use just one instance of them (really though python might be best here, or jq even better but I don't know jq very well). Here's a sed solution, just fill in a few more lines to save me the grunt work:

#!/bin/sh

curl $whatever | sed -n '
h
s/.*,status:\([^,]*\).*/${color}Status - ${color2}\1/p
g
s/.*domains_being_blocked:\([^,]*\).*/${color}Domains Blocked - ${color2}\1/p
g
s/.*,dns_queries_today:\([^,]*\).*/${color}DNS Queries Today - ${color2}\1/p
g
s/.*,ads_blocked_today:\([^,]*\).*/${color}Queries Blocked - ${color2}\1/p
'

EDIT: nevermind, python will not be great for this and jq will most likely not work at all as that input isn't actually json.  Is there a reason it isn't json?  It looks like it's intended to be, but isn't quite right.  Do you control the php script that generates it?  Can you modify the php script?

EDIT 2: oops, I just saw that the example input you gave is actually *after* that first sed command which seems intended to screw up the json ... why?  What is the output of the actual php script?  Use that, not some munged/broken version that you then try to 'unbreak'.

Assuming the php script generates valid json, this should work:

#!/bin/python

from json import loads
from urllib.request import urlopen

fmt = '''${{color}}Status - ${{color2}}{status}
${{color}}Domains Blocked - ${{color2}}{domains_being_blocked}
${{color}}DNS Queries Today - ${{color2}}{dns_queries_today}
...
'''

with urlopen('https://jessemcclure.org/~jmcclure/junk.in') as fd:
   data = loads(fd.read())
   print(fmt.format(**data))

If you control the php script, just modify it to send exactly what you want in the first place.

Last edited by Trilby (2019-09-20 01:14:09)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2019-09-20 02:06:42

merlock
Member
Registered: 2018-10-30
Posts: 235

Re: [SOLVED][conky + awk] Tighten up a script?

Trilby wrote:

Here's a sed solution...

Thank you, Trilby!  This is interesting; I've never seen (or used) the 'h' and 'g' command.

Trilby wrote:

EDIT 2: oops, I just saw that the example input you gave is actually *after* that first sed command which seems intended to screw up the json ... why?  What is the output of the actual php script?  Use that, not some munged/broken version that you then try to 'unbreak'.

I found the basic script on some conky thread somewhere, while I was looking for something else.  This is the unaltered output from the pi-hole:

{"domains_being_blocked":115967,"dns_queries_today":19506,"ads_blocked_today":2701,"ads_percentage_today":13.847021,"unique_domains":9228,"queries_forwarded":10707,"queries_cached":6091,"clients_ever_seen":488,"unique_clients":29,"dns_queries_all_types":19506,"reply_NODATA":1770,"reply_NXDOMAIN":8033,"reply_CNAME":4219,"reply_IP":5231,"privacy_level":0,"status":"enabled","gravity_last_updated":{"file_exists":true,"absolute":1568531829,"relative":{"days":"4","hours":"18","minutes":"04"}}}

The 'munging' just removes the quotation marks (probably easier for the original author to use awk), and capitalizes the word 'Enabled' (looks better).


Trilby wrote:

Assuming the php script generates valid json, this should work:

#!/bin/python

LOL.  I can barely do/understand shell scripting; it took me almost 3 days of searching to find the 'sub/gsub' commands for awk sad  Python (or anything else) would be a pipe-dream.

Trilby wrote:

If you control the php script, just modify it to send exactly what you want in the first place.

It's generated by the pi-hole software on my Raspberry Pi.  I probably won't be digging into that anytime in my foreseeable future.

Again, thank you.  I appreciate your time and insights!

(Going to mark as solved)


Eenie meenie, chili beanie, the spirits are about to speak -- Bullwinkle J. Moose
It's a big club...and you ain't in it -- George Carlin
Registered Linux user #149839
perl -e 'print$i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10); '

Offline

Board footer

Powered by FluxBB