We have so many application insights created in our environment. It’s not easy to see which application is reporting correctly. I want to have a report how many requests that the application is sent to Application Insights. This will help us to identify if there is a problem with integration or the application is no longer in production or might be it can help to detect the application is down for long time. Finally i come with a solution with PHP and Azure CLI . I create a file name report.php ( see the file at the bottom) . Here is the step to run:

#we need to login to azure first
bash#   az login
#then we can run our script
bash# php report.php
#the script will write to a file out.csv 
<?php
set_time_limit(0);
$OUTPUT_FILE="out.csv";

//We have multiple subscription so we have to run through all of them
$cmd="az account list";
$result = run_cmd ($cmd);
$subscription_list=json_decode($result);
@unlink($OUTPUT_FILE);
foreach($subscription_list as $subscription)
{
   debug("Switching to: $subscription->id,$subscription->name");
   run_cmd(" az account set --subscription $subscription->id");
   
   $cmd="az monitor app-insights component show";
   $result=run_cmd($cmd);
   $json=json_decode($result);
   if(!$json)
   {
      debug("Error..................");
      continue;
   }

    foreach ($json as $r)
    {
        $cmd="az monitor app-insights query --app $r->appId --analytics-query 'requests | summarize count() ' --offset 24h";
        $json_query=json_decode((shell_exec($cmd)));
        if(!$json_query)
        {
                debug("$cmd failed");
                continue;
        }
        $total_requests= ($json_query->tables[0]->rows[0][0]);
        $cmd="az monitor app-insights query --app $r->appId --analytics-query 'pageViews | summarize count() ' --offset 24h";
        $json_query=json_decode((shell_exec($cmd)));
        if(!$json_query)
        {
                debug("$cmd failed");
                continue;
        }
        $total_total_pageViews=  ($json_query->tables[0]->rows[0][0]);
        $str=("$subscription->id,$subscription->name,$r->resourceGroup,$r->appId,$r->applicationId , $r->applicationType,$r->connectionString,$total_requests,$total_total_pageViews ");
        debug($str);
        file_put_contents("$OUTPUT_FILE","\r\n$str",FILE_APPEND);
    }
}

//we need to output it to a tmp.txt folder is because , there might be some error, we do not want to display it
function run_cmd($cmd)
{
   $result=shell_exec("$cmd > tmp.txt 2>/dev/null");
   $result = file_get_contents("tmp.txt");
    return $result;
}

function debug($s)
{
        $d=date("Y-m-d H:i:s");
        echo "\r\n$d: $s";
}
?>