0

I need to parse the output of ldapsearch and only keep the attributes with numeric values.

Also I need to transform the output to make it usable in prometheus monitoring.

this is the output of a raw ldapsearch:

# 389, snmp, monitor
dn: cn=389,cn=snmp,cn=monitor
cn: 389
objectClass: top
objectClass: extensibleObject
anonymousbinds: 9
unauthbinds: 9
simpleauthbinds: 122256
strongauthbinds: 0
bindsecurityerrors: 27869
inops: 24501385
readops: 17933653
compareops: 24852
addentryops: 14205
removeentryops: 0
modifyentryops: 378287
modifyrdnops: 0
listops: 0
searchops: 19194674
onelevelsearchops: 117
wholesubtreesearchops: 1260904
referrals: 0
chainings: 0
securityerrors: 2343
errors: 4694375
connections: 1075
connectionseq: 4720927
bytesrecv: 1608469180
bytessent: -424079608
entriesreturned: 19299393
referralsreturned: 0

I execute this query in order to remove the fields that are not numerical and also the dn/cn fields if they have numbers eg cn=389.

${LDAPSEARCH} -LLL -H ${LDAP_URI} -x -D "${BINDDN}" -w ${LDAP_PASSWD} -b "${cn}" -s base | sed '/^cn\|^dn/d' |  awk -F: '{ if ( $1 != "connection" && $2 ~ /[[:digit:]$]/) printf "dsee_%s\n", $1 $2}'

But i need to modify the print f so that it prints me the field like this:

dsee_modifyrdnops{node="vm1",cn="389"} 0
dsee_listops{node="vm1",cn="1389"} 0
dsee_strongauthbinds{node="vm1",cn="389"} 0
dsee_readops{"node="vm1",cn="389"} 37194588

I have difficulties adding the curly brackets and quotes to the printf command.

what would be the best way to improve the awk/sed command and modify the printf output?

danidar
  • 179
  • 2
  • 10

2 Answers2

1

something along these lines:

$ cat tst.awk
BEGIN {
    FS=":[[:blank:]]*"
    qq="\""
    node="vm1"
}
$1=="cn" {cn=$2}
$1!~/^((cn|dn)$|connection)/ && $2~/^[[:digit:]]+$/ {
    printf("dsee_%s{node=%s%s%s,cn=%s%s%s} %d\n", $1, qq, node, qq, qq, cn, qq, $2)
}

$ awk -f tst.awk myFile
dsee_anonymousbinds{node="vm1",cn="389"} 9
dsee_unauthbinds{node="vm1",cn="389"} 9
dsee_simpleauthbinds{node="vm1",cn="389"} 122256
dsee_strongauthbinds{node="vm1",cn="389"} 0
dsee_bindsecurityerrors{node="vm1",cn="389"} 27869
dsee_inops{node="vm1",cn="389"} 24501385
dsee_readops{node="vm1",cn="389"} 17933653
dsee_compareops{node="vm1",cn="389"} 24852
dsee_addentryops{node="vm1",cn="389"} 14205
dsee_removeentryops{node="vm1",cn="389"} 0
dsee_modifyentryops{node="vm1",cn="389"} 378287
dsee_modifyrdnops{node="vm1",cn="389"} 0
dsee_listops{node="vm1",cn="389"} 0
dsee_searchops{node="vm1",cn="389"} 19194674
dsee_onelevelsearchops{node="vm1",cn="389"} 117
dsee_wholesubtreesearchops{node="vm1",cn="389"} 1260904
dsee_referrals{node="vm1",cn="389"} 0
dsee_chainings{node="vm1",cn="389"} 0
dsee_securityerrors{node="vm1",cn="389"} 2343
dsee_errors{node="vm1",cn="389"} 4694375
dsee_bytesrecv{node="vm1",cn="389"} 1608469180
dsee_entriesreturned{node="vm1",cn="389"} 19299393
dsee_referralsreturned{node="vm1",cn="389"} 0
vgersh99
  • 877
  • 1
  • 5
  • 9
1

In plain bash:

#!/bin/bash

node=vm1

while IFS=: read -r key val; do
    [[ $key = cn ]] && { cn=${val# }; continue; }
    if [[ $val =~ ^\ -?[0-9]+(\.[0-9]*)?$ ]]; then
        printf 'dsee_%s{node="%s",cn="%s"}%s\n' "$key" "$node" "$cn" "$val"
    fi
done < <( your_raw_ldapsearch_command )
M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
  • this works perfects but not when the val is a float eg: `bindetimesum: 10973.000317 bindetimesumsq: 2696.000000593497` – danidar Mar 10 '21 at 09:15
  • I tried this regex : ` $val =~ ^[+-]?[0-9]+\.?[0-9]*$` based on the examples here: https://stackoverflow.com/questions/13790763/bash-regex-to-check-floating-point-numbers-from-user-input but then nothing is printed. – danidar Mar 10 '21 at 10:58
  • @danidar Updated the answer so that the regular expression also matches the numbers with decimal points. – M. Nejat Aydin Mar 10 '21 at 11:52
  • a last question what does it mean the `#` in `cn=${val# }`? – danidar Mar 10 '21 at 14:17
  • @danidar It removes the leading space from the expansion of `val`. Read the section [Shell Parameter Expansion](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion) in the Bash Reference Manual for detailed information about all forms of parameter expansion. – M. Nejat Aydin Mar 10 '21 at 14:21
  • @danidar Note that the space following the `#` is significant. – M. Nejat Aydin Mar 10 '21 at 14:28