Announcement

Collapse
No announcement yet.

Daily email including all errors in the log over the past 24 hrs

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Daily email including all errors in the log over the past 24 hrs

    Does anyone know of an existing plugin or script that can do this? I found a log monitor, but I don't want to trigger on errors...at least not yet. I'd like to run a process once a day to gather up 24 hrs worth of errors and/or warnings, then email them.

    Thanks!
    HS4, Insteon, Z-wave, USB-UIRT, Harmony Hubs, Google Hub/Chromecasts/Speakers, Foscam & Amcrest cameras, EZVIZ DB1 doorbell
    Plugins: BLLAN, BLOccupied, BLUSBUIRT, Chromecast, Harmony Hub, Insteon, Jon00 Homeseer/Echo Skill Helper, Harmony Hub, Jon00 DB Charting, MediaController, NetCAM, PHLocation2, Pushover 3P, weatherXML, Z-wave

    #2
    I created my own little script to do this. In case anyone else is interested, I pasted it below. I setup an event to run this about every 24 hrs. You can pass in the number of hours you want to look backwards, otherwise it defaults to 25 hours. You can specify an array of words to look for within the log entries.

    Note I avoided the use of the word "error" in my log entries in this script, and for the script name, and for the event that runs it, to make sure the execution of this script does not appear in the daily emails.

    The script currently spits out log entries from newest to oldest. There are commented out lines of code if you want to reverse this.

    I use another script to send messages so you may want to adjust to use hs.SendEmail(). Example code provided.

    Code:
    Sub Main(ByVal param As String)
    
     ' For log entries
     Dim scriptName As String = "EmailErrLogEntries"
    
     ' Array of words to check log entries for
     Dim WordsArr(2) As String
     WordsArr(0) = "error"
     WordsArr(1) = "problem"
     WordsArr(2) = "issue"
    
     Dim numHours As Integer = 25
     If IsNumeric(param) Then
      numHours = CInt(param)
     End If
    
     Dim Logs() As HomeSeerAPI.LogEntry
     Logs = hs.GetLog_Date(Now.AddHours(numHours * -1), Now)
    
     If Logs IsNot Nothing AndAlso Logs.Count > 0 Then
    
      hs.WriteLog(scriptName, Logs.Count.ToString & " log entries in the past " & numHours & " hours.")
    
      Dim logLine As String = ""
      Dim errList As String = ""
      Dim wordList As String = ""
    
      ' For oldest to newest, use the line below instead of the following line; also change line further below
      'For Each Log As HomeSeerAPI.LogEntry In Logs
      For i As Integer = Logs.Count - 1 To 0 Step -1
       ' For oldest to newest, use the line below instead of the following line; also change line further above
       'logLine = "<tr><td nowrap>" & Log.LogTime & "</td><td>" & Log.LogType & "</td><td>" & Log.LogText & "</td></tr>"
       logLine = "<tr><td nowrap>" & Logs(i).LogTime & "</td><td>" & Logs(i).LogType & "</td><td>" & Logs(i).LogText & "</td></tr>"
       For Each word As String In WordsArr
        ' Build wordList only once
        If i = Logs.Count - 1 Then
         If wordList <> "" Then
          wordList = wordList & " or "
         End If
         wordList = wordList & """" & word & """"
        End If
        If InStr(1, logLine, word, vbTextCompare) > 0 Then
         errList = errList & logLine
         Exit For
        End If
       Next
      Next
    
      If errList <> "" Then
    
       hs.WriteLog(scriptName, "Sending err details.")
       errList = "Log entries containing " & wordList & " from the past " & numHours & " hours.<BR>" & Logs.Count.ToString & " log entries scanned.<br><br><table border=1><tr><td>Date/Time</td><td>Type</td><td>Message</td></tr>" & errList & "</table>"
    
       ' Send email via hs.SendEmail()
       ' Dim from As String = "from@domain.com"
       ' Dim toAddress As String = "to@domain.com"
       ' Dim cc As String = ""
       ' Dim Bcc As String = ""
       ' Dim subject As String = "HS Log Alerts - Reminder"
       ' Dim attachment As String = ""
       ' hs.SendEmail(toAddress, from, cc, Bcc, subject, errList, attachment)
    
       ' Send message via script
       Dim subject As String = "HS Log Alerts - Reminder"
       Dim sendTo As String = "$$DSR:359:"
       hs.RunScriptFunc("SendEmail.vb", "Main", sendTo & "|" & subject & "|" & errList, True, False)
    
      Else
    
       hs.WriteLog(scriptName, "No errs to send!")
    
      End If
    
     Else
    
      hs.WriteLog(scriptName, "*** PROBLEM DETECTED *** - no log entries to read")
    
     End If
    
    End Sub
    HS4, Insteon, Z-wave, USB-UIRT, Harmony Hubs, Google Hub/Chromecasts/Speakers, Foscam & Amcrest cameras, EZVIZ DB1 doorbell
    Plugins: BLLAN, BLOccupied, BLUSBUIRT, Chromecast, Harmony Hub, Insteon, Jon00 Homeseer/Echo Skill Helper, Harmony Hub, Jon00 DB Charting, MediaController, NetCAM, PHLocation2, Pushover 3P, weatherXML, Z-wave

    Comment


      #3
      That's nice ! I might try it soon. thanks

      Comment


        #4
        let's try this on linux :O

        Comment


          #5
          mrceolla can you explain the script a litle ? Is it sending the errors of the last day directly to the email configured in hs, or is it putting this into a devices string first? I didn't try the script yet.

          I am not a pro scripter.

          Comment


            #6
            Originally posted by MattL0 View Post
            let's try this on linux :O
            If only the log was a simple text file.

            Comment


              #7
              Sorry for the delay.

              If you use the hs.SendEmail stuff (currently commented out) then it will use whatever email addresses you supply, and send the email via HomeSeer's email settings. That is likely what most people would use. Please edit the file to enable those lines and comment out hs.RunScriptFunc

              This script needs to be run via an event with recurring trigger. Its only purpose is to scan the logs for the past X hours, check for matches, and email those matches. It does not store the results anywhere.

              This script is by no means perfect. I've been using it for a while now and so far so good. I'm getting some records emailed to me that contain my search words, but that I don't care about. I'm trying to think of a way to implement an exclusions list. I've also bolded the matched words recently. I may post an updated version if I do anything more with it.

              If anyone else makes any modifications, please share :-)

              HS4, Insteon, Z-wave, USB-UIRT, Harmony Hubs, Google Hub/Chromecasts/Speakers, Foscam & Amcrest cameras, EZVIZ DB1 doorbell
              Plugins: BLLAN, BLOccupied, BLUSBUIRT, Chromecast, Harmony Hub, Insteon, Jon00 Homeseer/Echo Skill Helper, Harmony Hub, Jon00 DB Charting, MediaController, NetCAM, PHLocation2, Pushover 3P, weatherXML, Z-wave

              Comment


                #8
                I've updated my little script to bold matched words, and also support exclusions via regex. I've also simplified the code a bit and added comments.

                Code:
                Sub Main(ByVal param As String)
                
                 Imports System.Text.RegularExpressions
                
                 ' For log entries generated by this script
                 Dim scriptName As String = "EmailErrLogEntries"
                
                 ' Array of words to check for in log entries
                 ' Adjust array size in declaration line as needed
                 Dim WordsArr(5) As String
                 WordsArr(0) = "error"
                 WordsArr(1) = "problem"
                 WordsArr(2) = "issue"
                 WordsArr(3) = "offline"
                 WordsArr(4) = "off line"
                 WordsArr(5) = "exception"
                
                 ' Array of Regex's to skip output of log entries even if they contain above words
                 ' Adjust array size in declaration line as needed
                 ' To Disable: comment out the string assignment lines below the line immediately below this line and set array size to zero in declaration
                 Dim ignoreRegexArr(1) As String
                 ignoreRegexArr(0) = "StopMediaPortal(.*)Exception(.*)actively refused"
                 ignoreRegexArr(1) = "Chromecast(.*)Error(.*)actively refused"
                
                 ' Use passed in value for hours if it's a number, otherwise default to 25
                 Dim numHours As Integer = 25
                 If IsNumeric(param) Then
                  numHours = CInt(param)
                 End If
                
                 Dim errList As String = ""
                
                 Dim Logs() As HomeSeerAPI.LogEntry
                 Logs = hs.GetLog_Date(Now.AddHours(numHours * -1), Now)
                
                 If Logs IsNot Nothing AndAlso Logs.Count > 0 Then
                
                  ' We have log entries
                  hs.WriteLog(scriptName, Logs.Count.ToString & " log entries in the past " & numHours & " hours.")
                
                  Dim logLine As String = ""
                  Dim logLineBolded As String = ""
                  Dim wordList As String = ""
                  Dim regexStr As String = ""
                
                  ' Build wordList and regexStr
                  For Each word As String In WordsArr
                   If wordList <> "" Then
                    wordList = wordList & " or "
                    regexStr = regexStr & "|"
                   End If
                   wordList = wordList & """" & word & """"
                   regexStr = regexStr & word
                  Next
                
                  ' For oldest to newest, use the line below instead of the following line; also change line further below
                  'For Each Log As HomeSeerAPI.LogEntry In Logs
                  For i As Integer = Logs.Count - 1 To 0 Step -1
                
                   ' For oldest to newest, use the line below instead of the following line; also change line further above
                   'logLine = "<tr><td nowrap>" & Log.LogTime & "</td><td>" & Log.LogType & "</td><td>" & Log.LogText & "</td></tr>"
                   logLine = "<tr><td nowrap>" & Logs(i).LogTime & "</td><td>" & Logs(i).LogType & "</td><td>" & Logs(i).LogText & "</td></tr>"
                
                   ' Put bold tags around any text that matches our search words
                   logLineBolded = Regex.Replace(logLine, "(" & regexStr & ")", "<b>$1</b>", RegexOptions.IgnoreCase)
                
                   ' If these two strings don't match, at least 1 word was bolded, so we have a match
                   If logLineBolded <> logLine Then
                    ' Check to make sure this isn't in our exclusion list
                    For Each ignoreRegex As String In ignoreRegexArr
                     ' This if statement is to prevent error if there are no ignoreRegexArr entries (if exclusions are disabled)
                     If ignoreRegex <> "" Then
                      Dim match As Match = Regex.Match(logLine, ignoreRegex, RegexOptions.IgnoreCase)
                      If match.Success Then
                       ' We want to ignore this record, so just blank out the variable; no need to analyze more so exit
                       logLineBolded = ""
                       Exit For
                      End If
                     End If
                    Next
                    ' Add this log entry to our collection to be emailed
                    errList = errList & logLineBolded
                   End If
                
                  Next
                
                  If errList <> "" Then
                
                   hs.WriteLog(scriptName, "Sending err details.")
                   ' Build surrounding text and HTML
                   errList = "Log entries containing " & wordList & " from the past " & numHours & " hours.<BR>" & Logs.Count.ToString & " log entries scanned.<br><br><table border=1><tr><td>Date/Time</td><td>Type</td><td>Message</td></tr>" & errList & "</table>"
                
                  Else
                
                   hs.WriteLog(scriptName, "No errs to send!")
                   errList = "No errors of concern!"
                
                  End If
                
                 Else
                
                  hs.WriteLog(scriptName, "*** PROBLEM DETECTED *** - no log entries to read")
                  errList = "*** PROBLEM DETECTED *** - no log entries to read"
                
                 End If
                
                 '' Send email via hs.SendEmail()
                 ' Dim from As String = "from@domain.com"
                 ' Dim toAddress As String = "to@domain.com"
                 ' Dim cc As String = ""
                 ' Dim Bcc As String = ""
                 ' Dim subject As String = "HS Log Alerts - Reminder"
                 ' Dim attachment As String = ""
                 ' hs.SendEmail(toAddress, from, cc, Bcc, subject, errList, attachment)
                
                 ' Send message via script
                 Dim subject As String = "HS Log Alerts - Reminder"
                 Dim sendTo As String = "$$DSR:359:"
                 hs.RunScriptFunc("SendEmail.vb", "Main", sendTo & "|" & subject & "|" & errList, True, False)  
                
                End Sub
                HS4, Insteon, Z-wave, USB-UIRT, Harmony Hubs, Google Hub/Chromecasts/Speakers, Foscam & Amcrest cameras, EZVIZ DB1 doorbell
                Plugins: BLLAN, BLOccupied, BLUSBUIRT, Chromecast, Harmony Hub, Insteon, Jon00 Homeseer/Echo Skill Helper, Harmony Hub, Jon00 DB Charting, MediaController, NetCAM, PHLocation2, Pushover 3P, weatherXML, Z-wave

                Comment


                  #9
                  I'm getting the following errors. Any ideas?
                  Compiling script /usr/local/HomeSeer/scripts/NotifyOnErrorsInLog.vb: 'Count' is not a member of 'HomeSeerAPI.LogEntry[]'.
                  Sep-26 3:26:57 PM Error Compiling script /usr/local/HomeSeer/scripts/NotifyOnErrorsInLog.vb: 'Count' is not a member of 'HomeSeerAPI.LogEntry[]'.
                  Sep-26 3:26:57 PM Error Compiling script /usr/local/HomeSeer/scripts/NotifyOnErrorsInLog.vb: 'Count' is not a member of 'HomeSeerAPI.LogEntry[]'.
                  Sep-26 3:26:57 PM Error Compiling script /usr/local/HomeSeer/scripts/NotifyOnErrorsInLog.vb: 'Count' is not a member of 'HomeSeerAPI.LogEntry[]'.
                  Sep-26 3:26:57 PM Error Compiling script /usr/local/HomeSeer/scripts/NotifyOnErrorsInLog.vb: The import 'System.Core' could not be found.

                  Comment


                    #10
                    Have you altered the script in any way aside from the arrays you're supposed to edit? Does it run as posted without errors?
                    HS4, Insteon, Z-wave, USB-UIRT, Harmony Hubs, Google Hub/Chromecasts/Speakers, Foscam & Amcrest cameras, EZVIZ DB1 doorbell
                    Plugins: BLLAN, BLOccupied, BLUSBUIRT, Chromecast, Harmony Hub, Insteon, Jon00 Homeseer/Echo Skill Helper, Harmony Hub, Jon00 DB Charting, MediaController, NetCAM, PHLocation2, Pushover 3P, weatherXML, Z-wave

                    Comment


                      #11
                      Originally posted by mrceolla View Post
                      Have you altered the script in any way aside from the arrays you're supposed to edit? Does it run as posted without errors?
                      I'm running it as is without any changes.

                      Comment


                        #12
                        HomeSeer 3? On Windows? Are you passing a parameter?
                        HS4, Insteon, Z-wave, USB-UIRT, Harmony Hubs, Google Hub/Chromecasts/Speakers, Foscam & Amcrest cameras, EZVIZ DB1 doorbell
                        Plugins: BLLAN, BLOccupied, BLUSBUIRT, Chromecast, Harmony Hub, Insteon, Jon00 Homeseer/Echo Skill Helper, Harmony Hub, Jon00 DB Charting, MediaController, NetCAM, PHLocation2, Pushover 3P, weatherXML, Z-wave

                        Comment


                          #13
                          Originally posted by mrceolla View Post
                          HomeSeer 3? On Windows? Are you passing a parameter?
                          HomeSeer 3? yes
                          Windows? No, Linux
                          Parameters? no

                          Thanks for your help in advance!!!

                          Comment


                            #14
                            Hmm, I'm guessing there is a difference with the log api in Linux. There was a comment made earlier in this thread about Linux. I wonder if that's why. do you know if that's the case?
                            HS4, Insteon, Z-wave, USB-UIRT, Harmony Hubs, Google Hub/Chromecasts/Speakers, Foscam & Amcrest cameras, EZVIZ DB1 doorbell
                            Plugins: BLLAN, BLOccupied, BLUSBUIRT, Chromecast, Harmony Hub, Insteon, Jon00 Homeseer/Echo Skill Helper, Harmony Hub, Jon00 DB Charting, MediaController, NetCAM, PHLocation2, Pushover 3P, weatherXML, Z-wave

                            Comment


                              #15
                              The Linux VB compiler used by HST is obsolete: https://forums.homeseer.com/forum/hs...bnc-deprecated

                              You can try changing the instances (four places) of '.Count' to '.Length'; that may allow a clean compile; though it may still fail for other reasons.

                              Alternately, if you're not opposed to hacking your mono install, you can substitute the more up-to-date vbc compiler in place of vbnc.

                              First make a backup of the original vbnc script
                              Code:
                              sudo install -p /usr/bin/vbnc /usr/bin/vbnc-dist
                              Then substitute the vbc script for vbnc
                              Code:
                              sudo install -p /usr/bin/vbc /usr/bin/vbnc

                              Comment

                              Working...
                              X