Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
franklupo committed May 30, 2022
1 parent 8b8dca6 commit 34b3582
Show file tree
Hide file tree
Showing 10 changed files with 793 additions and 803 deletions.
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ ClientBin/
*.publishsettings
orleans.codegen.cs

# Including strong name files can present a security risk
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk

Expand Down Expand Up @@ -317,7 +317,7 @@ __pycache__/
# OpenCover UI analysis results
OpenCover/

# Azure Stream Analytics local run output
# Azure Stream Analytics local run output
ASALocalRun/

# MSBuild Binary and Structured Log
Expand All @@ -326,5 +326,7 @@ ASALocalRun/
# NVidia Nsight GPU debugger configuration file
*.nvuser

# MFractors (Xamarin productivity tool) working folder
# MFractors (Xamarin productivity tool) working folder
.mfractor/

.parm
10 changes: 1 addition & 9 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/Corsinvest.ProxmoxVE.Metrics.Exporter/bin/Debug/net6.0/cv4pve-metrics-exporter.dll",
"args": [
"--host",
"10.92.90.101" ,
"--username",
"root",
"--password",
"campanacor321!1",
"prometheus"
],
"args": [],
"cwd": "${workspaceFolder}/src/Corsinvest.ProxmoxVE.Metrics.Exporter",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
Expand Down
677 changes: 663 additions & 14 deletions LICENSE.md

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ this software export metrics from Proxmox VE.
* Not require installation in Proxmox VE
* Execute out side Proxmox VE
* Use Api token --api-token parameter
* Execution with file parameter e.g. @FileParameter.parm

## Api token

Expand Down Expand Up @@ -96,3 +97,20 @@ For Prometheus are available more parameters:
--node-disk-info Export disk info (disk,wearout,smart)
Require more time
```

## Execution with file parameter

Is possible execute with file parameter

```sh
root@debian:~# cv4pve-metrics-exporter @FileParameter.parm
```

File **FileParameter.parm**

```txt
--host=192.168.0.100
--username=root@pam
--password=fagiano
prometheus
```
674 changes: 0 additions & 674 deletions gpl-3.0.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Version>1.4.0</Version>
<Version>1.4.1</Version>
<Company>Corsinvest Srl</Company>
<Authors>Daniele Corsini</Authors>
<Copyright>Corsinvest Srl</Copyright>
Expand Down Expand Up @@ -34,7 +34,7 @@

<ItemGroup>
<!-- <ProjectReference Include="..\..\..\cv4pve-api-dotnet\src\Corsinvest.ProxmoxVE.Api.Extension\Corsinvest.ProxmoxVE.Api.Extension.csproj" /> -->
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Extension" Version="3.1.3" />
<PackageReference Include="prometheus-net" Version="5.0.2" />
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Extension" Version="7.3.4" />
<PackageReference Include="prometheus-net" Version="6.0.0" />
</ItemGroup>
</Project>
89 changes: 58 additions & 31 deletions src/Corsinvest.ProxmoxVE.Metrics.Exporter.Api/PrometheusExporter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* SPDX-FileCopyrightText: 2019 Copyright Corsinvest Srl
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Corsinvest.ProxmoxVE.Api;
using Corsinvest.ProxmoxVE.Api.Extension;
using Corsinvest.ProxmoxVE.Api.Extension.Helpers;
using Corsinvest.ProxmoxVE.Api.Extension.Utils;
using Microsoft.Extensions.Logging;
using Prometheus;

namespace Corsinvest.ProxmoxVE.Metrics.Exporter.Api
Expand Down Expand Up @@ -57,6 +65,8 @@ public class PrometheusExporter
/// <param name="pveHostsAndPortHA"></param>
/// <param name="pveUsername"></param>
/// <param name="pvePassword"></param>
/// <param name="pveApiToken"></param>
/// <param name="loggerFactory"></param>
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="url"></param>
Expand All @@ -65,21 +75,24 @@ public class PrometheusExporter
public PrometheusExporter(string pveHostsAndPortHA,
string pveUsername,
string pvePassword,
string pveApiToken,
ILoggerFactory loggerFactory,
string host,
int port,
string url,
string prefix,
bool exportNodeDiskInfo)
: this(Prometheus.Metrics.NewCustomRegistry(), prefix, exportNodeDiskInfo)
{
_registry.AddBeforeCollectCallback(() =>
_registry.AddBeforeCollectCallback(async () =>
{
var stopwatch = new Stopwatch();
stopwatch.Start();

var client = ClientHelper.GetClientFromHA(pveHostsAndPortHA, null);
client.Login(pveUsername, pvePassword);
Collect(client);
var client = ClientHelper.GetClientFromHA(pveHostsAndPortHA);
client.LoggerFactory = loggerFactory;
if (string.IsNullOrWhiteSpace(pveApiToken)) { await client.Login(pveUsername, pvePassword); }
await Collect(client);

stopwatch.Stop();
});
Expand Down Expand Up @@ -232,17 +245,17 @@ private void CreateGauges(MetricFactory metricFactory, string prefix)
/// Collect data
/// </summary>
/// <param name="client"></param>
public void Collect(PveClient client)
public async Task Collect(PveClient client)
{
var aa = client.Version.Version();
SetGauge(_versionInfo, client.Version.Version().Response.data);
//todo fix in new version use decode model
SetGauge(_versionInfo, (await client.Version.Version()).Response.data);

var formatinfo = new NumberFormatInfo
var formatInfo = new NumberFormatInfo
{
NumberDecimalSeparator = "."
};

foreach (var item in client.Cluster.Status.GetStatus().ToEnumerable())
foreach (var item in (await client.Cluster.Status.GetStatus()).ToEnumerable())
{
switch (item.type)
{
Expand All @@ -253,7 +266,7 @@ public void Collect(PveClient client)
if (item.online == 1)
{
var node = item.name as string;
var data = client.Nodes[node].Status.Status().Response.data;
var data = (await client.Nodes[node].Status.Status()).Response.data;

foreach (var item1 in _nodeExtraInfo)
{
Expand All @@ -262,9 +275,9 @@ public void Collect(PveClient client)
"memory_used" => data.memory.used,
"memory_total" => data.memory.total,
"memory_free" => data.memory.free,
"load_avg1" => double.Parse(data.loadavg[0], formatinfo),
"load_avg5" => double.Parse(data.loadavg[1], formatinfo),
"load_avg15" => double.Parse(data.loadavg[2], formatinfo),
"load_avg1" => double.Parse(data.loadavg[0], formatInfo),
"load_avg5" => double.Parse(data.loadavg[1], formatInfo),
"load_avg15" => double.Parse(data.loadavg[2], formatInfo),
"swap_used" => data.swap.used,
"swap_total" => data.swap.total,
"swap_free" => data.swap.free,
Expand All @@ -276,21 +289,23 @@ public void Collect(PveClient client)
if (_exportNodeDiskInfo)
{
//disk info
foreach (var disk in client.Nodes[node].Disks.List.List().ToEnumerable())
foreach (var disk in (await client.Nodes[node].Disks.List.List()).ToEnumerable())
{
var labelValues = new string[] { disk.serial as string,
node,
disk.type as string,
disk.devpath as string };

var wearout = DynamicHelper.GetValue(disk, "wearout");
if (wearout is double wearoutD) { _nodeDiskHealth.WithLabels(labelValues).Set(wearoutD); }
if (ExistProperty(disk, "wearout") != null)
{
_nodeDiskHealth.WithLabels(labelValues).Set(GetValue<double>(disk, "wearout"));
}

_nodeDiskWearout.WithLabels(labelValues).Set((disk.health as string) == "PASSED" ? 1 : 0);

//smart
var smart = client.Nodes[node].Disks.Smart.Smart(disk.devpath as string).Response.data;
if (DynamicHelper.GetValue(smart, "attributes") != null)
var smart = (await client.Nodes[node].Disks.Smart.Smart(disk.devpath as string)).Response.data;
if (ExistProperty(smart, "attributes") != null)
{
foreach (var attribute in smart.attributes)
{
Expand All @@ -317,7 +332,7 @@ public void Collect(PveClient client)

//foreach (var item in client.Storage.Index().ToEnumerable()) { SetGauge1(StorageDef, item); }

foreach (var item in client.Cluster.Resources.Resources().ToEnumerable())
foreach (var item in (await client.Cluster.Resources.Resources()).ToEnumerable())
{
switch (item.type)
{
Expand All @@ -332,16 +347,16 @@ public void Collect(PveClient client)
{
var node = item.node as string;
var vmId = item.vmid + "" as string;
var data = (client.Nodes[node]
.Qemu[vmId]
.Monitor.Monitor("info balloon")
.Response.data as string)
var data = ((await client.Nodes[node]
.Qemu[vmId]
.Monitor.Monitor("info balloon"))
.Response.data as string)
.Split(" ")
.Skip(1)
.Select(a => new
{
Name = a.Split("=")[0],
Value = double.Parse(a.Split("=")[1], formatinfo)
Value = double.Parse(a.Split("=")[1], formatInfo)
});

foreach (var gbi in _guestBalloonInfo)
Expand All @@ -360,17 +375,29 @@ public void Collect(PveClient client)
default: break;
}

foreach (var item1 in _resourceInfo)
if (item.type == "qemu" || item.type == "lxc")
{
var value = DynamicHelper.GetValue(item, item1.Key);
if (value != null) { item1.Value.WithLabels(GetValues(item1.Value, item)).Set(value); }
foreach (var item1 in _resourceInfo)
{
item1.Value.WithLabels(GetValues(item1.Value, item))
.Set(GetValue<double>(item, item1.Key));
}
}
}
}

private static void SetGauge(Gauge gauge, dynamic values) => gauge.WithLabels(GetValues(gauge, values)).Set(1);
private static void SetGauge(Gauge gauge, dynamic values)
=> gauge.WithLabels(GetValues(gauge, values)).Set(1);

private static string[] GetValues(Gauge gauge, dynamic obj)
=> gauge.LabelNames.Select(a => DynamicHelper.GetValue(obj, a) + "").Cast<string>().ToArray();
=> gauge.LabelNames.Select(a => GetValue<object>(obj, a) + "")
.Cast<string>()
.ToArray();

private static bool ExistProperty(dynamic obj, string propertyName)
=> ((IDictionary<string, object>)obj).ContainsKey(propertyName);

private static T GetValue<T>(object obj, string propertyName)
=> (T)Convert.ChangeType(((IDictionary<string, object>)obj)[propertyName], typeof(T));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Version>1.3.0</Version>
<Version>1.4.0</Version>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>cv4pve-metrics-exporter</AssemblyName>
<Company>Corsinvest Srl</Company>
Expand All @@ -15,9 +15,9 @@

<ItemGroup>
<!-- <ProjectReference Include="..\..\..\cv4pve-api-dotnet\src\Corsinvest.ProxmoxVE.Api.Shell\Corsinvest.ProxmoxVE.Api.Shell.csproj" /> -->
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Shell" Version="3.1.2" />
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Shell" Version="7.3.4" />

<!-- <ProjectReference Include="..\Corsinvest.ProxmoxVE.Metrics.Exporter.Api\Corsinvest.ProxmoxVE.Metrics.Exporter.Api.csproj" /> -->
<PackageReference Include="Corsinvest.ProxmoxVE.Metrics.Exporter.Api" Version="1.4.0" />
<PackageReference Include="Corsinvest.ProxmoxVE.Metrics.Exporter.Api" Version="1.4.1" />
</ItemGroup>
</Project>
Loading

0 comments on commit 34b3582

Please sign in to comment.