Created
June 21, 2022 16:46
-
-
Save Rohitrajak1807/b1605d8ec8dc0fb8a58faa1e14ed9943 to your computer and use it in GitHub Desktop.
Fetching AKS ACI V Node vital stats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"context" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"strings" | |
_ "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" | |
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | |
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/operationalinsights/armoperationalinsights/v2" | |
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | |
) | |
var ( | |
subscriptionID string | |
objectID string | |
clientSecret string | |
location = "eastus" | |
resourceGroupName = "pf9-1655377915-na4i" | |
managedClustersName = "k8s-1655377915-na4i" | |
) | |
func main() { | |
subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID") | |
if len(subscriptionID) == 0 { | |
log.Fatal("AZURE_SUBSCRIPTION_ID is not set.") | |
} | |
objectID = os.Getenv("AZURE_OBJECT_ID") | |
if len(objectID) == 0 { | |
log.Fatal("AZURE_OBJECT_ID is not set.") | |
} | |
clientSecret = os.Getenv("AZURE_CLIENT_SECRET") | |
if len(clientSecret) == 0 { | |
log.Fatal("AZURE_CLIENT_SECRET is not set.") | |
} | |
cred, err := azidentity.NewDefaultAzureCredential(nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
ctx := context.Background() | |
t, err := cred.GetToken(ctx, policy.TokenRequestOptions{ | |
Scopes: []string{"https://api.loganalytics.io/.default"}, | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
client, err := armoperationalinsights.NewWorkspacesClient(subscriptionID, cred, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
res, err := client.Get(ctx, resourceGroupName, "dialtone-mon-dev", nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
url := "https://api.loganalytics.io/v1/workspaces/{workspace-id}/query" | |
url = strings.ReplaceAll(url, "{workspace-id}", *res.Workspace.Properties.CustomerID) | |
var body struct { | |
Query string `json:"query"` | |
} | |
// the query works. Tested it on the Azure portal | |
body.Query = ` | |
let subscriptionId = 'xxxxxxxxxxxxxxxxxxxxxxxx'; | |
let resourceGroup = 'pf9-1655377915-na4i'; | |
let clusterName = 'k8s-1655377915-na4i'; | |
let startDateTime = now(-1d); | |
let endDateTime = now(); | |
let clusterId = strcat('/subscriptions/', subscriptionId, '/resourceGroups/', resourceGroup, '/providers/Microsoft.ContainerService/managedClusters/', clusterName); | |
let memoryUsageCounterName = 'memoryRssBytes'; | |
let primaryInventory = KubePodInventory | | |
where TimeGenerated >= startDateTime | | |
where TimeGenerated < endDateTime | | |
where isnotempty(ClusterName) | | |
where isnotempty(Namespace) | | |
extend Node = Computer | | |
where ClusterId =~ clusterId | | |
where Node == 'virtual-node-aci-linux' | | |
project TimeGenerated, ClusterId, ClusterName, Namespace, ServiceName, Node = Computer, ControllerName, Pod = Name, ContainerInstance = ContainerName, ContainerID, InstanceName, PerfJoinKey = strcat(ClusterId, '/', ContainerName), ReadySinceNow = format_timespan(endDateTime - ContainerCreationTimeStamp, 'ddd.hh:mm:ss.fff'), Restarts = ContainerRestartCount, Status = ContainerStatus, ContainerStatusReason = columnifexists('ContainerStatusReason', ''), ControllerKind = ControllerKind, PodStatus, ControllerId = strcat(ClusterId, '/', Namespace, '/', ControllerName); | |
let latestContainersByController = primaryInventory | | |
where isnotempty(Node) | | |
summarize arg_max(TimeGenerated, *) by PerfJoinKey | | |
project ControllerId, PerfJoinKey; | |
let filteredMemoryUsage = Perf | | |
where TimeGenerated >= startDateTime | | |
where TimeGenerated < endDateTime | | |
where ObjectName == 'K8SContainer' | | |
where InstanceName startswith clusterId | | |
project TimeGenerated, CounterName, CounterValue, InstanceName, Node = Computer | | |
where Node == 'virtual-node-aci-linux'; | |
let memoryUsageByController = filteredMemoryUsage | | |
where CounterName =~ memoryUsageCounterName | | |
extend PerfJoinKey = InstanceName | | |
summarize Value = max(CounterValue) by PerfJoinKey, CounterName | | |
join (latestContainersByController) on PerfJoinKey | | |
summarize Value = sum(Value) by ControllerId, CounterName | | |
project ControllerId, CounterName, MemoryAggregationValue = Value; | |
let CPUUsageCounterName = 'cpuUsageNanoCores'; | |
let filteredCPUUsage = Perf | | |
where TimeGenerated >= startDateTime | | |
where TimeGenerated < endDateTime | | |
where ObjectName == 'K8SContainer' | | |
where InstanceName startswith clusterId | | |
project TimeGenerated, CounterName, CounterValue, InstanceName, Node = Computer | | |
where Node == 'virtual-node-aci-linux'; | |
let CPUUsageByController = filteredCPUUsage | | |
where CounterName =~ CPUUsageCounterName | | |
extend PerfJoinKey = InstanceName | | |
summarize Value = max(CounterValue) by PerfJoinKey, CounterName | | |
join (latestContainersByController) on PerfJoinKey | | |
summarize Value = sum(Value) by ControllerId, CounterName | | |
project ControllerId, CounterName, CPUAggregationValue = Value/1000000; | |
let maxMemoryUsage = primaryInventory | | |
distinct ControllerId, ControllerName, ControllerKind, Namespace | | |
join kind=leftouter (memoryUsageByController) on ControllerId | | |
project MaxMemoryRSS = MemoryAggregationValue, ControllerId; | |
let maxCPUUsage = primaryInventory | | |
distinct ControllerId, ControllerName, ControllerKind, Namespace | | |
join kind=leftouter (CPUUsageByController) on ControllerId | | |
project MaxCPUUsage = CPUAggregationValue, ControllerId; | |
maxMemoryUsage | | |
join(maxCPUUsage) on ControllerId | | |
project MaxCPUUsage, MaxMemoryRSS;` | |
bodyBytes, err := json.Marshal(body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
bytes.NewBuffer(bodyBytes) | |
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(bodyBytes)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
req.Method = http.MethodPost | |
req.Header.Add("Content-Type", "application/json") | |
req.Header.Add("authorization", fmt.Sprintf("Bearer %s", t.Token)) | |
response, err := http.DefaultClient.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
responseBody, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(responseBody)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment