patricegautier / unifiZabbix

Zabbix templates to monitor pretty much all Unifi devices
184 stars 36 forks source link

Port description #37

Closed zpolisensky closed 2 years ago

zpolisensky commented 2 years ago

This isn't really a problem, more of an idea for improvement. With Cisco switches I am used to have in Zabbix in the port name a description directly from the switch. It's handy because when an alarm comes up, I know if I should start panicking or if it's just an unimportant port. I only found the port description in "show run", so I added a couple of lines to the mca-dump-short.sh script to pull it from the switch and add it to the final json. In order not to burden the switch unnecessarily, I only update the data once per hour. I'm no programmer, so please don't be intimidated by my code :)

if [[ ${DEVICE_TYPE:-} == 'SWITCH' ]]; then
   SWportCount=$(echo ${OUTPUT} | jq .port_table[].port_idx | tail -1)

   getRunConf=false
   if [[ ! -f /tmp/unifiSWconf.log ]]; then
        getRunConf=true
   else
        SWconfTimeStamp=$(tail -1 /tmp/unifiSWconf.log)
        timeStampHourAgo=$(date -d "1 hour ago" "+%s")
        if [ "${SWconfTimeStamp}" -lt "${timeStampHourAgo}" ]; then
           getRunConf=true
        fi
   fi

   if ${getRunConf}; then
      /usr/bin/expect >/dev/null 2>&1<<EOD
      set timeout 10

      spawn ssh -o LogLevel=Error -o StrictHostKeyChecking=accept-new ${PRIVKEY_OPTION} ${USER}@${TARGET_DEVICE}

      expect ".*#"
      send -- "telnet 127.0.0.1\r"

      expect "(UBNT) >"
      send -- "enable\r"

      expect "(UBNT) #"
      send -- "terminal length 0\r"

      expect "(UBNT) #"
      send -- "show run\r"
      log_file -noappend /tmp/unifiSWconf.log;

      expect "(UBNT) #"
      send -- "exit\r"
      log_file;

      expect "(UBNT) >"
      send -- "exit\r"

      expect ".*#"
      send -- "exit\r"

      expect eof
EOD

      echo -e "\n$(date +%s)">>/tmp/unifiSWconf.log

   fi

   for (( i=1; i<=${SWportCount}; i++ )); do
      portDesc=$(sed -n "/interface 0\/${i}\b/,/exit/p" /tmp/unifiSWconf.log | grep description | awk -F"'" '{ print $2 }')
      indexNo=$((i-1))
      OUTPUT=$(echo ${OUTPUT} | jq -r ".port_table[${indexNo}] += { \"port_desc\": \"${portDesc}\" }")
   done
fi

echo "$OUTPUT"

This is how I can see port name in Zabbix then ("Camera" is a port description from the switch): "Port 1 - Camera - Connection Speed"

And thank you for the great work on this project!

patricegautier commented 2 years ago

Thanks for the enhancement! Definitely a good addition; I will work this one in shortly, stay tuned

patricegautier commented 2 years ago

@zpolisensky some switches do not seem to support the telnet API you are using, e.g USW Flex. I guess it's because they are based on a different underlying platform. Do you know how to tell them apart from the mca-dump output per chance?

zpolisensky commented 2 years ago

I use it with these switches: US-8-60W, US-48-500W, USW-Pro-48-PoE, US-16-150W, US-24-250W. Unfortunately I don't have any Flex switches.

patricegautier commented 2 years ago

Hi there - checkout today's commit.. it integrates and expands on your idea to get more names from more switches. Caveat though there are a lot of other changes I had been working on, preparing for a new major releases. The only switch I am not able to support there is the USW-Flex..

zpolisensky commented 2 years ago

Hi @patricegautier, I've been busy lately, but I finally got around to trying it out. Everything looks good and works, here are just a few notes:

Thank you for your work Zbynek

patricegautier commented 2 years ago

Hi @zpolisensky thanks for the feedback!

script in Ubuntu throws an error because of the interpreter: /usr/local/bin/bash: bad interpreter: No such file or directory. I edited it to /bin/bash and ok.

committed that change

It took me a while to figure out that I need to add two new macros {$UNIFI_CHECK_TIMEOUT} and {$UNIFI_DISCOVERY_FREQUENCY}. It would be good to add them to the readme.

added to doc

A problem unrelated to your update, but it might help someone. In Ubuntu 20.04 the "ssh.run" key does not work with the standard libssh. Just install the latest version from PPA and everything is OK.

also added to doc.

Thanks and closing this one..