Ad Widget

Collapse

Count directory files for current day

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markfree
    Senior Member
    • Apr 2019
    • 868

    #1

    Count directory files for current day

    Hey guys.
    I need some help with monitoring the number of XML files in a Windows directory.
    For that, I have tried this item key.
    Code:
    vfs.dir.count["E:\dir\path","\d+.xml",,file,,0,,,,1d]
    This should count the XML files that match the regular expression "\d+.xml" in the specified directory. However, I only want to count the files that were created or modified today, and ignore the previous ones.
    I know that the "max_age" parameter from the "vfs.dir.count" key can filter the files by their age, but It does not support a time shift (1d:now/d+1d) for the current day.

    Invalid parameters: strconv.ParseInt: parsing "1d:now/d+1": invalid syntax.


    Does anyone have a better solution for counting files in a directory for the current day?
    Last edited by markfree; 15-08-2023, 16:13.
  • Hamardaban
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • May 2019
    • 2713

    #2
    1)
    Code:
    "\d+\.xml"

    2) modification time only
    3) no shift at all

    You can make your own script that will count the amount you need and transfer the data to zabbix.

    Comment

    • markfree
      Senior Member
      • Apr 2019
      • 868

      #3
      That's a shame!
      Since there's no native solution, a simple PowerShell script was created for this task.
      Code:
      $dateToday = Get-Date -Format "yyyy-MM-dd"
      $fileCount = (Get-ChildItem -Path "C:\directory\path" -File | Where-Object { $_.CreationTime.Date -eq $dateToday }).Count
      Write-Host $fileCount
      The item's key:
      Code:
      system.run[powershell.exe -nologo -noprofile "$dateToday = Get-Date -Format \"yyyy-MM-dd\"; $fileCount = (Get-ChildItem -Path \"C:\directory\path\" -File | Where-Object { $_.CreationTime.Date -eq $dateToday }).Count; Write-Host $fileCount;"]
      Last edited by markfree; 20-08-2023, 15:55.

      Comment

      • cyber
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Dec 2006
        • 4807

        #4
        Not having a way to use any dates (generated on the fly) in items has always been a weak point..
        One can work around it by generating some global macros every midnight with needed values and then use those, but its workaround...

        Comment


        • tim.mooney
          tim.mooney commented
          Editing a comment
          Oh, that idea about using macros and updating them every midnight is a fantastic technique! Thanks for mentioning that.
      • markfree
        Senior Member
        • Apr 2019
        • 868

        #5
        Originally posted by cyber
        Not having a way to use any dates (generated on the fly) in items has always been a weak point..
        One can work around it by generating some global macros every midnight with needed values and then use those, but its workaround...
        Could you elaborate more on that? How would you do that?

        Comment

        • cyber
          Senior Member
          Zabbix Certified SpecialistZabbix Certified Professional
          • Dec 2006
          • 4807

          #6
          Create a script type item (or external check) , which generates a date, formats it as you need and updates macro(s) over API. you can create as many as you need, in any shape or form... also "yesterday" or "tomorrow" are possible.. Assign to your server, run on schedule every midnight.
          {$GLOBAL.TODAY.DDMMYYYY}
          {$GLOBAL.TODAY.YYYYMMDD}
          {$GLOBAL.YESTERDAY.DDMMYYYY}
          {$GLOBAL.YESTERDAY.YYYYMMDD}

          I think we tested it once with an item (system.localtime[local]) , where all the JS stuff for all this was in preprocessing, as there was no script type items in v4.4
          Last edited by cyber; 18-08-2023, 09:56.

          Comment

          • markfree
            Senior Member
            • Apr 2019
            • 868

            #7
            I'm not sure that would fit the "max age" parameter for "vfs.dir.count" key because it seems to rely on a seconds counter rather than a regular date format.
            For instance, if I were to set up a macro like "{$TODAY.SECONDS}", it looks like I'd have to keep updating it constantly to match the exact number of seconds in the current date.

            So, my question is: has anyone tried using such a macro with this key before? How did you manage to keep it up to date with the ever-changing second count? I'm really curious to know if there's a smart way to make this work seamlessly and accurately within the context of the "vfs.dir.count" key."

            Comment

            • cyber
              Senior Member
              Zabbix Certified SpecialistZabbix Certified Professional
              • Dec 2006
              • 4807

              #8
              yeah.. with that max_age it will not help... drifted away from topic...:P

              Comment

              • markfree
                Senior Member
                • Apr 2019
                • 868

                #9
                If only the {TIME} macro could be used in items...
                Code:
                {{TIME}.fmttime(%y%m%d)}

                Comment

                Working...