Page 1 of 10

HowTo: monitor Synology NAS

Posted: Sunday 12 January 2014 14:05
by rvmourik
Now in the WIKI: http://www.domoticz.com/wiki/NAS_Monitoring

In addition to the post of mbliek about setting up monitoring for a QNAP NAS, here is the how to for a Synology NAS.

I have used most of the info from his topic (http://www.domoticz.com/forum/viewtopic.php?f=17&t=1337), so the credits are for him :-)

Below you'll find the how-to:

First enable SNMP on your Synology NAS:

You can find the option in the Configuration Screen, it's a sub-option in "Network Services".

You can use V1/V2
And set a password (Community)

Then install SNMP on your RaspberryPi

sudo apt-get install snmpd
sudo apt-get install snmp

Reboot Pi

Check if SNMP is up and running:

Code: Select all

snmpget -c PASSWORD -v2c -O qv NASIPADDRESS .1.3.6.1.4.1.6574.1.2.0
The result will be an Integer giving you the temperature in Degrees Celcius, in my case it was:

"40"

Every device (I.E. Qnap or Synology has it own implementation layer regarding commands for SNMP, you can use the documentation together with the command line tool snmpwalk to get more information on which codes to use to get a specific output for temperature of used disk space. Below you'll find an example using snmpwalk to get more information:

Code: Select all

snmpwalk -c PASSWORD -v2c -O qv NASIPADDRESS .1.3.6.1.4.1.6574.1.2

Ouput will be someting like:

iso.3.6.1.4.1.6574.1.2.0 = INTEGER: 40

as you can see, you need to use the additional ".0" at the end to get the temperature of your Synology NAS.
Make 3 new virtual Temperature Sensors (NAS HD1, NAS HD2 and NAS Free HD)
And 1 new virtual Switch (NAS)

nas.sh (in domoticz/scripts/)

Code: Select all

#!/bin/bash

# Settings

NASIP="0.0.0.0"         # NAS IP Address
PASSWORD="password"         # SNMP Password
DOMO_IP="0.0.0.0"      # Domoticz IP Address
DOMO_PORT="0000"         # Domoticz Port

NAS_IDX="16"               # NAS Switch IDX
NAS_HD1_TEMP_IDX="14"        # NAS HD1 Temp IDX
NAS_HD2_TEMP_IDX="13"        # NAS HD2 Temp IDX
NAS_HD_SPACE_IDX="15"        # NAS HD1 Temp IDX

# Check if NAS in online 

PINGTIME=`ping -c 1 -q $NASIP | awk -F"/" '{print $5}' | xargs`

echo $PINGTIME
if expr "$PINGTIME" '>' 0
then
       echo "NAS ON"
       # Send data
        curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$NAS_IDX&switchcmd=On"

        # Temperature HD1
        HDtemp1=`snmpget -v 2c -c $PASSWORD -O qv $NASIP 1.3.6.1.4.1.6574.2.1.1.6.0`
        # Send data
        curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$NAS_HD1_TEMP_IDX&nvalue=0&svalue=$HDtemp1"

        # Temperature HD2
        HDtemp2=`snmpget -v 2c -c $PASSWORD -O qv $NASIP 1.3.6.1.4.1.6574.2.1.1.6.1`
        # Send data
        curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$NAS_HD2_TEMP_IDX&nvalue=0&svalue=$HDtemp2"

        # Free space volume
        HDUnit=`snmpget -v 2c -c $PASSWORD -O qv $NASIP 1.3.6.1.2.1.25.2.3.1.4.36`
        HDTotal=`snmpget -v 2c -c $PASSWORD -O qv $NASIP 1.3.6.1.2.1.25.2.3.1.5.36`
        HDUsed=`snmpget -v 2c -c $PASSWORD -O qv $NASIP 1.3.6.1.2.1.25.2.3.1.6.36`
        HDFree=$((($HDTotal - $HDUsed) * $HDUnit / 1024 / 1024 / 1024))

        # Send data
        curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$NAS_HD_SPACE_IDX&nvalue=0&svalue=$HDFree"

else
       echo "NAS OFF"
       # Send data
          curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$NAS_IDX&switchcmd=Off"

fi

Change the idx to your own idx

Make this script executable:
sudo chmod +x nas.sh

Try the script out:
./nas.sh

You should get something like:

Code: Select all

0.809
1
NAS ON
HTTP/1.0 200 OK
Content-Length: 51
Content-Type: text/html;charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache

{
   "status" : "OK",
   "title" : "SwitchLight"
}
HTTP/1.0 200 OK
Content-Length: 53
Content-Type: text/html;charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache

{
   "status" : "OK",
   "title" : "Update Device"
}
HTTP/1.0 200 OK
Content-Length: 53
Content-Type: text/html;charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache

{
   "status" : "OK",
   "title" : "Update Device"
}
HTTP/1.0 200 OK
Content-Length: 53
Content-Type: text/html;charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache

{
   "status" : "OK",
   "title" : "Update Device"
}

Make a new crontab job:
crontab -e
*/5 * * * * /home/pi/domoticz/scripts/nas.sh (= every 5 minutes)

Forgot to say
I did made a room called NAS and put all my virtual sensors and switch there.

Thats it, any question? Just ask.

As mbliek mentioned in his topic:
Forgot to say
I did made a room called NAS and put all my virtual sensors and switch there.
This is it for now, if you have any questions let me know. And again the credits go out to the original poster mbliek with his topic:

http://www.domoticz.com/forum/viewtopic.php?f=17&t=1337

07-02-14 change Changed a hardcoded number into a variable, thanks to BigDog for noticing

Re: HowTo: monitor Synology NAS

Posted: Wednesday 29 January 2014 16:44
by rvmourik
No one using it besides myself? :o

Re: HowTo: monitor Synology NAS

Posted: Wednesday 29 January 2014 18:36
by Yihaaa
Well I like to have this! Very interesting, the only thing is that I am not an expert with Linux.

Re: HowTo: monitor Synology NAS

Posted: Wednesday 29 January 2014 19:48
by BigDog
I want to use it havent got the time yet to add it to domoticz :)

Re: HowTo: monitor Synology NAS

Posted: Thursday 30 January 2014 12:40
by BigDog
the nas server SNMP requested a username and a password
in the script it only shows a password
where do i put the username?
or isn't that necessary?

Re: HowTo: monitor Synology NAS

Posted: Thursday 30 January 2014 16:40
by rvmourik
BigDog wrote:the nas server SNMP requested a username and a password
in the script it only shows a password
where do i put the username?
or isn't that necessary?
From the top of my head: you're trying to use the newest version of SNMP with Synology. When you're using V2 it only uses a password. I'm not exactly sure how to implement the newer version.

Re: HowTo: monitor Synology NAS

Posted: Thursday 30 January 2014 16:48
by mbliek
Thats true, you are using V3 then.

This line should be edited then:

snmpget -v 2c -c $PASSWORD -O qv $NASIP 1.3.6.1.4.1.6574.2.1.1.6.0

Don't know how, you should use google for this.
Or change to V2 and you don't need a username.

Re: HowTo: monitor Synology NAS

Posted: Thursday 30 January 2014 20:27
by BigDog
i have dubbelchecked it if i acticate SNMP and put a V on V2 i cant enter password only "gemeenschap" (dutch version) and below my information contact person and stuff i dont know if i dont set a pass ... is that a security isue? or if someone sees the snmp of the nas they only can see the temp and free storage nothing else?

Re: HowTo: monitor Synology NAS

Posted: Thursday 30 January 2014 21:25
by mbliek
You can set your own password at v2

Re: HowTo: monitor Synology NAS

Posted: Thursday 30 January 2014 22:09
by BigDog
Ohhhh wait "gemeenschap" is the password... I didnt know that....:|

Re: HowTo: monitor Synology NAS

Posted: Thursday 30 January 2014 22:15
by mbliek
A bit wrong translated.

Re: HowTo: monitor Synology NAS

Posted: Friday 31 January 2014 10:50
by Luuc_a
Thanks for sharing. This is realy nice and works great.

Re: HowTo: monitor Synology NAS

Posted: Friday 31 January 2014 17:39
by geho
I used this script and it works like a charm. Would be great if we can use GB as unit.

Re: HowTo: monitor Synology NAS

Posted: Thursday 06 February 2014 9:40
by BigDog
strange my raspberry cant start the script nas.sh :S

i get this

Code: Select all

XX@XXXXXX ~/domoticz/scripts $ ./nas.sh
bash: ./nas.sh: /bin/bash^M: bad interpreter: No such file or directory
what am i doing wrong :D

Re: HowTo: monitor Synology NAS

Posted: Thursday 06 February 2014 10:11
by BigDog
have stromble to another strange thing when i run the script with sudo it works but then i get this :S

Code: Select all

pi@XXXXXXX ~ $ sudo bash domoticz/scripts/nas.sh
domoticz/scripts/nas.sh: line 2: $'\r': command not found
domoticz/scripts/nas.sh: line 4: $'\r': command not found
domoticz/scripts/nas.sh: line 9: $'\r': command not found
domoticz/scripts/nas.sh: line 14: $'\r': command not found
domoticz/scripts/nas.sh: line 16: $'\r': command not found
domoticz/scripts/nas.sh: line 18: $'\r': command not found
0.731
domoticz/scripts/nas.sh: line 52: syntax error: unexpected end of file

and the nas a virtual switch ? when i make one in domoticz you can only add it as a temp a counter and stuff but

Re: HowTo: monitor Synology NAS

Posted: Thursday 06 February 2014 12:58
by tkimber
Your nas.sh script is in DOS format instead of unix format. You need to convert the \r\n characters to \n characters. This can be done with the 'tr' command, e.g.

tr -d '\015' <DOS-file >UNIX-file

More details at StackOverflow: http://stackoverflow.com/questions/2613 ... ash-script

Re: HowTo: monitor Synology NAS

Posted: Thursday 06 February 2014 19:42
by BigDog
Thanks it works :D :D :D

i had only a question about the free space volume i see only the free space of HD1 like 398 degrades i dont see de HD2 and not the total/used/units and stuff
cant it be made that you see all the info and in MB or GB?

Re: HowTo: monitor Synology NAS

Posted: Friday 07 February 2014 7:26
by BigDog
hi there i found a Bug in the script

else
echo "NAS OFF"
# Send data
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=184&switchcmd=Off"

fi
the highlighted part must be $NAS_IDX :)

i saw it when the button dasn't go off :)

glad that i can help 8-)

Re: HowTo: monitor Synology NAS

Posted: Friday 07 February 2014 10:37
by rvmourik
BigDog wrote:hi there i found a Bug in the script

else
echo "NAS OFF"
# Send data
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=184&switchcmd=Off"

fi
the highlighted part must be $NAS_IDX :)

i saw it when the button dasn't go off :)

glad that i can help 8-)
thanks, i've changed the start topic.

Re: HowTo: monitor Synology NAS

Posted: Wednesday 12 February 2014 22:19
by mbliek
I made a Wiki page for the NAS monitoring.
I added the link in the first post.

http://www.domoticz.com/wiki/NAS_Monitoring

I edited your script a bit @rvmourik