Method to automatically email if a drive goes offline (or a USB port is restarted)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















Is there a quick and easy method to send an email (I already have automatic emails coming from the server for other events - I just need to be able to send one for this specific event) if a USB port restarts and, by extension, an external drive goes offline?



I installed a new CCTV system in my father in law's house last year, and as an added bonus, I bought him a Raspberry PI, set it up as a VPN and file server, and set the cameras to directly record on motion to some NFS shares I've set up on an external drive on the PI so that there were two copies of anything important if someone had the brains to steal the NVR.



But the cameras kept losing the storage and required a reboot to bring the storage back online.



I assumed this was a POE issue, as the cameras themselves were also sometimes rebooting when powered directly from the NVR, but after installing a POE switch and changing the network config a bit, that has ceased.



I checked today though and found the cameras had again lost their secondary storage. Investigating further, I narrowed down the time, and checked the syslog on the PI. I found that it had suffered a USB issue.




usb 1-1.1-port2: disabled by hub (EMI?), re-enabling...




This was followed by thousands of messages regarding the drives going offline, coming online, and the cameras attempting to access the NFS shares.



What I want is for the system to ping me an email if this event is logged to the syslog file, or simply when one of the filesystems goes offline, so I can ring him to see if he's doing anything specific.



It may be something as simply as a dodgy USB lead or drive enclosure (we got a self powered enclosure to save the PI from having to spin a 3.5" HDD) so it may even be the PSU, but I'd like to try to identify anything happening while the failures are occurring before getting a new HDD enclosure that may not solve the issue.



The PI itself is powered by the official Pi 3 psu. The casing of the PI is aluminium (I can't remember the specific one I bought but it's all metal to act as a heat sink) but it's fairly hidden away in behind the monitor in his study so it's unlikely to be getting shifted about much and shorting against anything.



Edit at request of K7AAY



The Pi is running Raspbian 9



I will be modifying the following script if the solution involves a method that can run a script.



#bin/sh
IPADDRESS=$(curl https://wtfismyip.com/text)
if [ "$IPADDRESS" != "$(cat /home/will/scripts/.current_ip)" ]
then
dt=$(date)
echo "Your new IP address as of $dt is $IPADDRESS r r FTB" | mail -s "IvanPI IP address change" **********@gmail.com
echo "$IPADDRESS" >| /home/will/scripts/.current_ip
echo "$dt $IPADDRESS" >> /home/will/scripts/.iphistory
fi


the only command I need to issue if the above event occurs will be




echo "External HDD offline/USB restarted at $dt r r FTB" | mail -s "IvanPI HDD Offline" *************@gmail.com




I just need to know how to trigger that command to occur if the HDD goes offline/Filesystem goes offline/USB port restarts.



Any of these events would be suitable to generate the trigger for the email, since they're all essentially the same thing.










share|improve this question




























    1















    Is there a quick and easy method to send an email (I already have automatic emails coming from the server for other events - I just need to be able to send one for this specific event) if a USB port restarts and, by extension, an external drive goes offline?



    I installed a new CCTV system in my father in law's house last year, and as an added bonus, I bought him a Raspberry PI, set it up as a VPN and file server, and set the cameras to directly record on motion to some NFS shares I've set up on an external drive on the PI so that there were two copies of anything important if someone had the brains to steal the NVR.



    But the cameras kept losing the storage and required a reboot to bring the storage back online.



    I assumed this was a POE issue, as the cameras themselves were also sometimes rebooting when powered directly from the NVR, but after installing a POE switch and changing the network config a bit, that has ceased.



    I checked today though and found the cameras had again lost their secondary storage. Investigating further, I narrowed down the time, and checked the syslog on the PI. I found that it had suffered a USB issue.




    usb 1-1.1-port2: disabled by hub (EMI?), re-enabling...




    This was followed by thousands of messages regarding the drives going offline, coming online, and the cameras attempting to access the NFS shares.



    What I want is for the system to ping me an email if this event is logged to the syslog file, or simply when one of the filesystems goes offline, so I can ring him to see if he's doing anything specific.



    It may be something as simply as a dodgy USB lead or drive enclosure (we got a self powered enclosure to save the PI from having to spin a 3.5" HDD) so it may even be the PSU, but I'd like to try to identify anything happening while the failures are occurring before getting a new HDD enclosure that may not solve the issue.



    The PI itself is powered by the official Pi 3 psu. The casing of the PI is aluminium (I can't remember the specific one I bought but it's all metal to act as a heat sink) but it's fairly hidden away in behind the monitor in his study so it's unlikely to be getting shifted about much and shorting against anything.



    Edit at request of K7AAY



    The Pi is running Raspbian 9



    I will be modifying the following script if the solution involves a method that can run a script.



    #bin/sh
    IPADDRESS=$(curl https://wtfismyip.com/text)
    if [ "$IPADDRESS" != "$(cat /home/will/scripts/.current_ip)" ]
    then
    dt=$(date)
    echo "Your new IP address as of $dt is $IPADDRESS r r FTB" | mail -s "IvanPI IP address change" **********@gmail.com
    echo "$IPADDRESS" >| /home/will/scripts/.current_ip
    echo "$dt $IPADDRESS" >> /home/will/scripts/.iphistory
    fi


    the only command I need to issue if the above event occurs will be




    echo "External HDD offline/USB restarted at $dt r r FTB" | mail -s "IvanPI HDD Offline" *************@gmail.com




    I just need to know how to trigger that command to occur if the HDD goes offline/Filesystem goes offline/USB port restarts.



    Any of these events would be suitable to generate the trigger for the email, since they're all essentially the same thing.










    share|improve this question


























      1












      1








      1








      Is there a quick and easy method to send an email (I already have automatic emails coming from the server for other events - I just need to be able to send one for this specific event) if a USB port restarts and, by extension, an external drive goes offline?



      I installed a new CCTV system in my father in law's house last year, and as an added bonus, I bought him a Raspberry PI, set it up as a VPN and file server, and set the cameras to directly record on motion to some NFS shares I've set up on an external drive on the PI so that there were two copies of anything important if someone had the brains to steal the NVR.



      But the cameras kept losing the storage and required a reboot to bring the storage back online.



      I assumed this was a POE issue, as the cameras themselves were also sometimes rebooting when powered directly from the NVR, but after installing a POE switch and changing the network config a bit, that has ceased.



      I checked today though and found the cameras had again lost their secondary storage. Investigating further, I narrowed down the time, and checked the syslog on the PI. I found that it had suffered a USB issue.




      usb 1-1.1-port2: disabled by hub (EMI?), re-enabling...




      This was followed by thousands of messages regarding the drives going offline, coming online, and the cameras attempting to access the NFS shares.



      What I want is for the system to ping me an email if this event is logged to the syslog file, or simply when one of the filesystems goes offline, so I can ring him to see if he's doing anything specific.



      It may be something as simply as a dodgy USB lead or drive enclosure (we got a self powered enclosure to save the PI from having to spin a 3.5" HDD) so it may even be the PSU, but I'd like to try to identify anything happening while the failures are occurring before getting a new HDD enclosure that may not solve the issue.



      The PI itself is powered by the official Pi 3 psu. The casing of the PI is aluminium (I can't remember the specific one I bought but it's all metal to act as a heat sink) but it's fairly hidden away in behind the monitor in his study so it's unlikely to be getting shifted about much and shorting against anything.



      Edit at request of K7AAY



      The Pi is running Raspbian 9



      I will be modifying the following script if the solution involves a method that can run a script.



      #bin/sh
      IPADDRESS=$(curl https://wtfismyip.com/text)
      if [ "$IPADDRESS" != "$(cat /home/will/scripts/.current_ip)" ]
      then
      dt=$(date)
      echo "Your new IP address as of $dt is $IPADDRESS r r FTB" | mail -s "IvanPI IP address change" **********@gmail.com
      echo "$IPADDRESS" >| /home/will/scripts/.current_ip
      echo "$dt $IPADDRESS" >> /home/will/scripts/.iphistory
      fi


      the only command I need to issue if the above event occurs will be




      echo "External HDD offline/USB restarted at $dt r r FTB" | mail -s "IvanPI HDD Offline" *************@gmail.com




      I just need to know how to trigger that command to occur if the HDD goes offline/Filesystem goes offline/USB port restarts.



      Any of these events would be suitable to generate the trigger for the email, since they're all essentially the same thing.










      share|improve this question
















      Is there a quick and easy method to send an email (I already have automatic emails coming from the server for other events - I just need to be able to send one for this specific event) if a USB port restarts and, by extension, an external drive goes offline?



      I installed a new CCTV system in my father in law's house last year, and as an added bonus, I bought him a Raspberry PI, set it up as a VPN and file server, and set the cameras to directly record on motion to some NFS shares I've set up on an external drive on the PI so that there were two copies of anything important if someone had the brains to steal the NVR.



      But the cameras kept losing the storage and required a reboot to bring the storage back online.



      I assumed this was a POE issue, as the cameras themselves were also sometimes rebooting when powered directly from the NVR, but after installing a POE switch and changing the network config a bit, that has ceased.



      I checked today though and found the cameras had again lost their secondary storage. Investigating further, I narrowed down the time, and checked the syslog on the PI. I found that it had suffered a USB issue.




      usb 1-1.1-port2: disabled by hub (EMI?), re-enabling...




      This was followed by thousands of messages regarding the drives going offline, coming online, and the cameras attempting to access the NFS shares.



      What I want is for the system to ping me an email if this event is logged to the syslog file, or simply when one of the filesystems goes offline, so I can ring him to see if he's doing anything specific.



      It may be something as simply as a dodgy USB lead or drive enclosure (we got a self powered enclosure to save the PI from having to spin a 3.5" HDD) so it may even be the PSU, but I'd like to try to identify anything happening while the failures are occurring before getting a new HDD enclosure that may not solve the issue.



      The PI itself is powered by the official Pi 3 psu. The casing of the PI is aluminium (I can't remember the specific one I bought but it's all metal to act as a heat sink) but it's fairly hidden away in behind the monitor in his study so it's unlikely to be getting shifted about much and shorting against anything.



      Edit at request of K7AAY



      The Pi is running Raspbian 9



      I will be modifying the following script if the solution involves a method that can run a script.



      #bin/sh
      IPADDRESS=$(curl https://wtfismyip.com/text)
      if [ "$IPADDRESS" != "$(cat /home/will/scripts/.current_ip)" ]
      then
      dt=$(date)
      echo "Your new IP address as of $dt is $IPADDRESS r r FTB" | mail -s "IvanPI IP address change" **********@gmail.com
      echo "$IPADDRESS" >| /home/will/scripts/.current_ip
      echo "$dt $IPADDRESS" >> /home/will/scripts/.iphistory
      fi


      the only command I need to issue if the above event occurs will be




      echo "External HDD offline/USB restarted at $dt r r FTB" | mail -s "IvanPI HDD Offline" *************@gmail.com




      I just need to know how to trigger that command to occur if the HDD goes offline/Filesystem goes offline/USB port restarts.



      Any of these events would be suitable to generate the trigger for the email, since they're all essentially the same thing.







      filesystems usb logs email external-hdd






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 8 at 0:28







      Will

















      asked Feb 7 at 22:31









      WillWill

      1085




      1085




















          1 Answer
          1






          active

          oldest

          votes


















          1














          Trafikito and monit are reasonably lightweight services which can monitor your drive, and if it fails, go check the USB port status. Then, either can mail out a message. Suggest you review their feature sets and see which you prefer.






          share|improve this answer























          • Monit looks as if it will do exactly what I need if the filesystem has an error. Thank you.

            – Will
            Feb 9 at 22:55











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499369%2fmethod-to-automatically-email-if-a-drive-goes-offline-or-a-usb-port-is-restarte%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Trafikito and monit are reasonably lightweight services which can monitor your drive, and if it fails, go check the USB port status. Then, either can mail out a message. Suggest you review their feature sets and see which you prefer.






          share|improve this answer























          • Monit looks as if it will do exactly what I need if the filesystem has an error. Thank you.

            – Will
            Feb 9 at 22:55
















          1














          Trafikito and monit are reasonably lightweight services which can monitor your drive, and if it fails, go check the USB port status. Then, either can mail out a message. Suggest you review their feature sets and see which you prefer.






          share|improve this answer























          • Monit looks as if it will do exactly what I need if the filesystem has an error. Thank you.

            – Will
            Feb 9 at 22:55














          1












          1








          1







          Trafikito and monit are reasonably lightweight services which can monitor your drive, and if it fails, go check the USB port status. Then, either can mail out a message. Suggest you review their feature sets and see which you prefer.






          share|improve this answer













          Trafikito and monit are reasonably lightweight services which can monitor your drive, and if it fails, go check the USB port status. Then, either can mail out a message. Suggest you review their feature sets and see which you prefer.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 8 at 0:23









          K7AAYK7AAY

          660624




          660624












          • Monit looks as if it will do exactly what I need if the filesystem has an error. Thank you.

            – Will
            Feb 9 at 22:55


















          • Monit looks as if it will do exactly what I need if the filesystem has an error. Thank you.

            – Will
            Feb 9 at 22:55

















          Monit looks as if it will do exactly what I need if the filesystem has an error. Thank you.

          – Will
          Feb 9 at 22:55






          Monit looks as if it will do exactly what I need if the filesystem has an error. Thank you.

          – Will
          Feb 9 at 22:55


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499369%2fmethod-to-automatically-email-if-a-drive-goes-offline-or-a-usb-port-is-restarte%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown






          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay