Ad Widget

Collapse

Hyper V Replication Auto Discovery Help - First Time

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LukeAB93UK
    Member
    Zabbix Certified Specialist
    • Jun 2023
    • 72

    #1

    Hyper V Replication Auto Discovery Help - First Time

    Hello,

    So I am attempting to use a Powershell script to pull in the Hyper V Replication Status.

    Here is my Powershell Script and the line in my Zabbix Agent Config.

    HTML Code:
    # Function to get Hyper-V VM replication status and output LLD JSON data
    function Get-HyperVReplicationStatus {
        $vms = Get-VM
        
        $lldData = @()
        foreach ($vm in $vms) {
            $replication = Get-VMReplication -VMName $vm.VMName -ErrorAction SilentlyContinue
            
            $vmData = @{
                "{#VMNAME}" = $vm.VMName
                "ReplicationEnabled" = if ($replication) { $true } else { $false }
                "ReplicationMode" = if ($replication) { $replication.ReplicationMode } else { "Disabled" }
                "ReplicationState" = if ($replication) { $replication.ReplicationState } else { "N/A" }
                "HealthStatus" = if ($replication) { $replication.Health } else { "N/A" }
            }
            $lldData += $vmData
        }
        
        $lldJson = $lldData | ConvertTo-Json -Depth 100
        Write-Output $lldJson
    }
    
    # Get Hyper-V VM replication status and output LLD JSON data
    Get-HyperVReplicationStatus
    ​
    HTML Code:
    UserParameter=vm.replication.status[*],powershell -NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\Zabbix Agent\scripts\Get-HyperVReplicationStatus.ps1" "$1"
    Now from my Zabbix Server I can run the "vm.replication.status" key and I get a list of all of the VMs on our Hypervisor Server and it looks like this.

    HTML Code:
    # zabbix_get -s <IP ADDRESS> -k vm.replication.status
    [
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  4,
            "ReplicationMode":  2,
            "ReplicationEnabled":  true,
            "HealthStatus":  1,
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        },
        {
            "ReplicationState":  "N/A",
            "ReplicationMode":  "Disabled",
            "ReplicationEnabled":  false,
            "HealthStatus":  "N/A",
            "{#VMNAME}":  "<REDACTED>"
        }
    ]
    ​
    So far so good. I am getting Replication State, Replicaton Mode, Replication Enabled and Health Status.

    Now what I want is to create an auto discovery rule with 4x items that say

    Replication State - VM Name
    Replication Mode - VM Name
    Replication Enabled - VM Name
    Health Status - VM Name

    But I am struggling to make it work. I breifly had it pulling in and I changed something and I was a little discracted and it stopped working.

    Here is what I have so far.

    Here is the discovery rule on my template in our test environment.

    Click image for larger version

Name:	image.png
Views:	1672
Size:	34.8 KB
ID:	468173
    Click image for larger version

Name:	image.png
Views:	1440
Size:	20.9 KB
ID:	468174

    And here is the Item Prototype I made.

    Click image for larger version

Name:	image.png
Views:	1430
Size:	39.4 KB
ID:	468175

    When I apply the template to the Hypervisor host I am getting the following error.

    Click image for larger version

Name:	image.png
Views:	1467
Size:	15.2 KB
ID:	468176

    This is my first time writing a Discovery Rule from scratch and not modifying or going off of someone elses work so I am a little lost and I know I have messed up somewhere but I cant see where I messed up.

    Can someone help me with this discovery rule.
    ​​​
  • Answer selected by LukeAB93UK at 03-08-2023, 15:37.
    SanMonitor
    Member
    • Aug 2022
    • 48

    Originally posted by LukeAB93UK

    Hello,

    So I made my Discovery rule a dependant item on the Item in the template.



    I am still getting this error. "Cannot create item: item with the same key "vm.replication.status[{#VMNAME}]" already exists."

    Here are my configuration screenshots. I know I am going wrong somewhere but I cant pin point where.

    Master Item
    Click image for larger version  Name:	image.png Views:	0 Size:	27.2 KB ID:	468283

    Discovery Rule
    ​​
    Click image for larger version  Name:	image.png Views:	0 Size:	23.5 KB ID:	468284​​
    Click image for larger version  Name:	image.png Views:	0 Size:	16.9 KB ID:	468285
    Item Protoype​
    Click image for larger version  Name:	image.png Views:	0 Size:	35.0 KB ID:	468286
    ​​​
    Hey Luke,
    can you try to change these things? I'm just basing this on my templates (which also took me a while to get right):

    LLD Macro
    {#VMNAME} = $.["VMName"]

    Item Prototype
    Change Type of Information to Text > I'm thinking the "N/A" might cause trouble otherwise​

    Comment

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

      #2
      But you have nowhere declared the "vm.replication.discovery" key... you only have "vm.replication.status" in your agent config...

      For me it looks like your "vm.replication.status" already returns all the data necessary, so you should maybe call it for discovery and then all the item prototypes should be dependent items, which take data from that main discovery item (preprocessing, jsonpath etc).

      Comment

      • LukeAB93UK
        Member
        Zabbix Certified Specialist
        • Jun 2023
        • 72

        #3
        Originally posted by cyber
        But you have nowhere declared the "vm.replication.discovery" key... you only have "vm.replication.status" in your agent config...

        For me it looks like your "vm.replication.status" already returns all the data necessary, so you should maybe call it for discovery and then all the item prototypes should be dependent items, which take data from that main discovery item (preprocessing, jsonpath etc).
        Okay so I made the Auto discovery rule use "vm.replication.status"

        Click image for larger version

Name:	image.png
Views:	1439
Size:	19.6 KB
ID:	468250

        This is my Item Prototype

        Click image for larger version

Name:	image.png
Views:	1425
Size:	31.1 KB
ID:	468251

        But it is just spitting out this error on the host I apply the template too

        Click image for larger version

Name:	image.png
Views:	1425
Size:	49.5 KB
ID:	468252

        I am not sure what I have done wrong. Both the discovery rule and the Item Protype are using vm.replication status.

        I have tried both the LLD Macro

        {#VMNAME} - $.discovery[*].VMName

        and

        {#VMNAME} - $.VMName

        The data is there but I cant get it to make seperate entries on each VM for

        "ReplicationState":
        "ReplicationMode":
        "ReplicationEnabled":
        "HealthStatus":​

        Comment

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

          #4
          Item prototypes type should be dependent item, not zabbix agent.. thats for first thing...
          second.. don't use {#VMNAME} as field name in script.. use ordinary "VMName" there ("VMName" = $vm.VMName)... Then your LLD assignment also works.
          third.. item prototype preprocessing... jsonpath and "$.[?(@.VMName == "{#VMNAME}")].ReplicationState.first()" for example... that should return ReplicationState value for each VM, when it iterates over data...
          And then you can create similar prototypes for other 3 things also...

          Have not done it for a while.. And it has always given me headaches..

          Comment

          • LukeAB93UK
            Member
            Zabbix Certified Specialist
            • Jun 2023
            • 72

            #5
            Originally posted by cyber
            Item prototypes type should be dependent item, not zabbix agent.. thats for first thing...
            second.. don't use {#VMNAME} as field name in script.. use ordinary "VMName" there ("VMName" = $vm.VMName)... Then your LLD assignment also works.
            third.. item prototype preprocessing... jsonpath and "$.[?(@.VMName == "{#VMNAME}")].ReplicationState.first()" for example... that should return ReplicationState value for each VM, when it iterates over data...
            And then you can create similar prototypes for other 3 things also...

            Have not done it for a while.. And it has always given me headaches..
            Hello,

            Thank you for the advice. So i have updated my script.

            I am trying to make my Item Prototype dependant on the Auto-discovery rule however

            Click image for larger version  Name:	image.png Views:	0 Size:	19.2 KB ID:	468271
            Nothing shows up I cant selet my discovery rule as the master item.​

            Edit: Just realised I need a standard item to be the master not the discovery rule
            Last edited by LukeAB93UK; 03-08-2023, 11:58.

            Comment

            • LukeAB93UK
              Member
              Zabbix Certified Specialist
              • Jun 2023
              • 72

              #6
              Originally posted by cyber
              Item prototypes type should be dependent item, not zabbix agent.. thats for first thing...
              second.. don't use {#VMNAME} as field name in script.. use ordinary "VMName" there ("VMName" = $vm.VMName)... Then your LLD assignment also works.
              third.. item prototype preprocessing... jsonpath and "$.[?(@.VMName == "{#VMNAME}")].ReplicationState.first()" for example... that should return ReplicationState value for each VM, when it iterates over data...
              And then you can create similar prototypes for other 3 things also...

              Have not done it for a while.. And it has always given me headaches..
              Right so.

              I have a master item using the key "vm.replication.status". this is working and pulling in the raw output.

              Click image for larger version

Name:	image.png
Views:	1446
Size:	37.1 KB
ID:	468273

              Now I re-created my discovery rule with the key vm.replication.rule because I can not have 2x items with the same key.

              Finally I made a dependant item prototype dependant on the item using vm.replication.status with the key vm.replication.status[{#VMNAME}] and the item protoype has a json preprocessing step of "$.[?(@.VMName == "{#VMNAME}")].ReplicationState.first()" and an LLD macro of {#VMNAME} - $.discovery[*].VMName.

              and I am still getting Unsupported Item Key

              Click image for larger version

Name:	image.png
Views:	1427
Size:	11.9 KB
ID:	468274

              Comment

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

                #7
                Discovery key -> vm.replication.status
                Key is the thing that is asked from agent, as you have only "vm.replication.status" defined there, then you need to use that...

                EDIT: oh wait.. if you have master item and your discovery uses that data also, then change discovery type also to "dependent"....
                Last edited by cyber; 03-08-2023, 13:09.

                Comment

                • SanMonitor
                  Member
                  • Aug 2022
                  • 48

                  #8
                  Originally posted by LukeAB93UK

                  Right so.

                  I have a master item using the key "vm.replication.status". this is working and pulling in the raw output.

                  Click image for larger version  Name:	image.png Views:	2 Size:	37.1 KB ID:	468273

                  Now I re-created my discovery rule with the key vm.replication.rule because I can not have 2x items with the same key.

                  Finally I made a dependant item prototype dependant on the item using vm.replication.status with the key vm.replication.status[{#VMNAME}] and the item protoype has a json preprocessing step of "$.[?(@.VMName == "{#VMNAME}")].ReplicationState.first()" and an LLD macro of {#VMNAME} - $.discovery[*].VMName.

                  and I am still getting Unsupported Item Key

                  Click image for larger version  Name:	image.png Views:	2 Size:	11.9 KB ID:	468274
                  Hey Luke,
                  The last screenshot is of your discovery rule with key vm.replication.rule? The Type here should be dependent item, not Zabbix agent.
                  The reason you're getting the error is because the key vm.replication.rule is unknown to the Zabbix Agent.

                  An overview:
                  Template has an item with key vm.replication.status that returns JSON, type Zabbix Agent.
                  Then you should have a discovery rule type Dependent item (on the previously created item) with your key vm.replication.rule.
                  ​Under this you can create your item prototype, also type dependent.

                  Comment

                  • LukeAB93UK
                    Member
                    Zabbix Certified Specialist
                    • Jun 2023
                    • 72

                    #9
                    Originally posted by SanMonitor

                    Hey Luke,
                    The last screenshot is of your discovery rule with key vm.replication.rule? The Type here should be dependent item, not Zabbix agent.
                    The reason you're getting the error is because the key vm.replication.rule is unknown to the Zabbix Agent.

                    An overview:
                    Template has an item with key vm.replication.status that returns JSON, type Zabbix Agent.
                    Then you should have a discovery rule type Dependent item (on the previously created item) with your key vm.replication.rule.
                    ​Under this you can create your item prototype, also type dependent.
                    Hello,

                    So I made my Discovery rule a dependant item on the Item in the template.



                    I am still getting this error. "Cannot create item: item with the same key "vm.replication.status[{#VMNAME}]" already exists."

                    Here are my configuration screenshots. I know I am going wrong somewhere but I cant pin point where.

                    Master Item
                    Click image for larger version

Name:	image.png
Views:	1447
Size:	27.2 KB
ID:	468283

                    Discovery Rule
                    ​​
                    Click image for larger version

Name:	image.png
Views:	1433
Size:	23.5 KB
ID:	468284​​
                    Click image for larger version

Name:	image.png
Views:	1429
Size:	16.9 KB
ID:	468285
                    Item Protoype​
                    Click image for larger version

Name:	image.png
Views:	1432
Size:	35.0 KB
ID:	468286
                    ​​​

                    Comment

                    • SanMonitor
                      Member
                      • Aug 2022
                      • 48

                      #10
                      Originally posted by LukeAB93UK

                      Hello,

                      So I made my Discovery rule a dependant item on the Item in the template.



                      I am still getting this error. "Cannot create item: item with the same key "vm.replication.status[{#VMNAME}]" already exists."

                      Here are my configuration screenshots. I know I am going wrong somewhere but I cant pin point where.

                      Master Item
                      Click image for larger version  Name:	image.png Views:	0 Size:	27.2 KB ID:	468283

                      Discovery Rule
                      ​​
                      Click image for larger version  Name:	image.png Views:	0 Size:	23.5 KB ID:	468284​​
                      Click image for larger version  Name:	image.png Views:	0 Size:	16.9 KB ID:	468285
                      Item Protoype​
                      Click image for larger version  Name:	image.png Views:	0 Size:	35.0 KB ID:	468286
                      ​​​
                      Hey Luke,
                      can you try to change these things? I'm just basing this on my templates (which also took me a while to get right):

                      LLD Macro
                      {#VMNAME} = $.["VMName"]

                      Item Prototype
                      Change Type of Information to Text > I'm thinking the "N/A" might cause trouble otherwise​

                      Comment

                      • LukeAB93UK
                        Member
                        Zabbix Certified Specialist
                        • Jun 2023
                        • 72

                        #11
                        Originally posted by SanMonitor

                        Hey Luke,
                        can you try to change these things? I'm just basing this on my templates (which also took me a while to get right):

                        LLD Macro
                        {#VMNAME} = $.["VMName"]

                        Item Prototype
                        Change Type of Information to Text > I'm thinking the "N/A" might cause trouble otherwise​
                        You my friend are a genius! It works. Thank you.

                        Click image for larger version

Name:	image.png
Views:	1419
Size:	24.7 KB
ID:	468290

                        Comment

                        • xavigray
                          Junior Member
                          • Nov 2024
                          • 1

                          #12
                          I have tried to replicate your process, but every time I have the same problem when assigning my template to a host.> This problem "Cannot create item: item with the same key "vm.replication.status[{#VMNAME}]" already exists."

                          I would like to put here some print-screens of how I was building, I don't know if I missed any steps.

                          I tried to fix it myself, but I'm still a newbie with zabbix and I was stuck with this error fighting with it all day, I already deleted my discovery rule/item several times :/

                          Click image for larger version

Name:	image.png
Views:	679
Size:	28.7 KB
ID:	494651
                          Click image for larger version

Name:	image.png
Views:	666
Size:	25.4 KB
ID:	494652Click image for larger version

Name:	image.png
Views:	667
Size:	22.3 KB
ID:	494653
                          Click image for larger version

Name:	image.png
Views:	663
Size:	9.8 KB
ID:	494654
                          Click image for larger version

Name:	image.png
Views:	663
Size:	22.7 KB
ID:	494655


                          Comment

                          • koflert
                            Junior Member
                            • Aug 2024
                            • 5

                            #13
                            Thanks for the great discussion, I was looking for exactly this solution. So that others don't have to go through the "build process", please find it as a final solution at GitHub.


                            Comment

                            Working...