none
Powershell foreach problem with if statement-Solved RRS feed

  • Question

  • I'm trying to get metric from Azure to Zabbix.

    The issue is that Metric for VM consists of 2 words:Percentage CPU,and Zabbix doesn't allow item keys to consists of 2 words. I Also tried Percentage%20CPU but getting errors in Zabbix, and i created Zabbix key percentage_cpu

    So i decided prior sending data from Zabbix to Azure to "translate" percentage_cpu to Percentage%20CPU this works great if only that key is present, but issue starts when i add another key (in this example SQL metric).

    For SQL metric all values are in one word-no need to change anything, but then metric for VM is also assigned to SQL.I'm trying to avoid writing separate file for every service

       
     $host_items = Get-ZabbixHostItems -url $zabbix_url -auth $zabbix_auth - 
     zabbix_host $host_name  
        foreach ($host_item in $host_items) 
    
        { 
    
            #$host_item_details = select-string -InputObject $host_item.key_ -Pattern '^(azure\.sql)\.(.*)\.(.*)\[\"(.*)\"\]$'; 
    
            $host_item_details = select-string -InputObject $host_item.key_ -Pattern '^(azure\.\w{2,})\.(.*)\.(.*)\[\"(.*)\"\,(.*)]$'; 
    
            #$host_item_details = select-string -InputObject $host_item.key_ -Pattern '^(azure)\.(.*)\.(.*)\.(.*)\[\"(.*)\"\,(.*)]$'; 
    
            $host_item_provider = $host_item_details.Matches.groups[1]; 
    
            $host_item_metric = $host_item_details.Matches.groups[2]; 
    
            $host_item_timegrain = $host_item_details.Matches.groups[3]; 
    
            $host_item_resource = $host_item_details.Matches.groups[4]; 
    
            $host_item_suffix = $host_item_details.Matches.groups[5]; 
    
    
    
    
             if ($host_item_metric='percentage_cpu')
              {$host_item_metric='Percentage%20CPU'}
              else
    
              { $host_item_metric = $host_item_details.Matches.groups[2];}
          #}
    
    
    
            $uri = "https://management.azure.com{0}/providers/microsoft.insights/metrics?api-version={1}&interval={2}&timespan={3}&metric={4}" -f ` 
    
                $host_item_resource, ` 
    
                "2017-05-01-preview", ` 
    
                $host_item_timegrain.ToString().ToUpper(), ` 
    
                $($(get-date).ToUniversalTime().addminutes(-15).tostring("yyyy-MM-ddTHH:mm:ssZ") + "/" + $(get-date).ToUniversalTime().addminutes(-2).tostring("yyyy-MM-ddTHH:mm:ssZ")), ` 
    
    
                $host_item_metric; 
    
    
    
    
            write-host $uri; 
    
    
          }


    output of hostitems_

    azure.sql.dtu_consumption_percent.pt1m["/subscriptions/111-222/resourceGroups/rg/providers/Microsoft.Sql/servers/mojsql/databases/test",common]
    azure.vm.percentage_cpu.pt1m["/subscriptions/111-222/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/test",common]


    When i ran code above i'm getting these URI's

    https://management.azure.com/subscriptions/111-222/resourceGroups/rg/providers/Microsoft.Sql/servers/mojsql/databases/test/providers/microsoft.insights/m
    etrics?api-version=2017-05-01-preview&interval=PT1M&timespan=2018-08-11T07:38:05Z/2018-08-11T07:51:05Z&metric=Percentage%20CPU
    
    
    https://management.azure.com/subscriptions/111-222/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/test/providers/microsoft.insights/metric
    s?api-version=2017-05-01-preview&interval=PT1M&timespan=2018-08-11T07:38:05Z/2018-08-11T07:51:05Z&metric=Percentage%20CPU
    
    
    


    For first link (SQL) metric should be dtu_consumption but i'm getting same metric for both links
    





    • Edited by Dragan979 Saturday, August 11, 2018 10:05 AM
    Saturday, August 11, 2018 9:34 AM

Answers

  • got it working with -like statement
    • Marked as answer by Dragan979 Saturday, August 11, 2018 10:52 AM
    Saturday, August 11, 2018 10:05 AM

All replies

  • Modify if ($host_item_metric='percentage_cpu') to if ($host_item_metric -eq 'percentage_cpu')
    Saturday, August 11, 2018 9:48 AM
  • doesn't work, getting original values:

    dtu_consumption_percent

    percentage_cpu



    • Edited by Dragan979 Saturday, August 11, 2018 9:54 AM
    Saturday, August 11, 2018 9:54 AM
  • got it working with -like statement
    • Marked as answer by Dragan979 Saturday, August 11, 2018 10:52 AM
    Saturday, August 11, 2018 10:05 AM
  • For Zabbix issues post here: https://www.zabbix.com/

    Your code is badly broken as posted and almost impossible to follow.

    Here is a  cleanup with some obvious fixes.  There is no way we can understand what you are trying to do.  You need to isolate a single issue and try to get an answer for that.  We cannot help with the Zabbix issues.

    $template = 'https://management.azure.com{0}/providers/microsoft.insights/metrics?api-version={1}&interval={2}&timespan={3}&metric={4}'
    $host_items = Get-ZabbixHostItems -url $zabbix_url -auth $zabbix_auth -zabbix_host $host_name  
    foreach ($host_item in $host_items){ 
        $host_item_details = select-string -InputObject $host_item.key_ -Pattern '^(azure\.\w{2,})\.(.*)\.(.*)\[\"(.*)\"\,(.*)]$'
        $host_item_provider = $host_item_details.Matches.groups[1]
        $host_item_metric = $host_item_details.Matches.groups[2]
        $host_item_timegrain = $host_item_details.Matches.groups[3]
        $host_item_resource = $host_item_details.Matches.groups[4] 
        $host_item_suffix = $host_item_details.Matches.groups[5]
        if ($host_item_metric -eq 'percentage_cpu'){
            $host_item_metric = 'Percentage%20CPU'
        }else{
            $host_item_metric = $host_item_details.Matches.groups[2]
        }
        $times = $($(get-date).ToUniversalTime().addminutes(-15).tostring("yyyy-MM-ddTHH:mm:ssZ") + "/" + $(get-date).ToUniversalTime().addminutes(-2).tostring("yyyy-MM-ddTHH:mm:ssZ"))
        $uri = $template -f $host_item_resource,'2017-05-01-preview', $host_item_timegrain.ToString().ToUpper(),$times,$host_item_metric 
        write-host $uri; 
    }

    This eliminates some of the obvious errors and makes the code somewhat readable.


    \_(ツ)_/



    • Edited by jrv Saturday, August 11, 2018 10:19 AM
    Saturday, August 11, 2018 10:15 AM
  • @jrv,

    i had only problem with for loop Powershell code, i though i clearly stated it. I mentioned Zabbix just to explain what i want to do 

    Saturday, August 11, 2018 10:24 AM
  • Then it is fixed.  Mark the answer.  Try to post readable code in the future and try to ask a clear question.  If Zabbix has nothing to do with the issue then don't discuss Zabbix and all of the other issues. 

    \_(ツ)_/

    Saturday, August 11, 2018 10:27 AM
  • my question was very clear: i got same values in foreach loop in powershell script, and provided script output as a proof, and explained what it does.
    Saturday, August 11, 2018 10:51 AM