Skip to content

Commit

Permalink
Merge pull request #342 from hubblo-org/336-proper-handling-of-window…
Browse files Browse the repository at this point in the history
…s-service-management

WIP: proper handling of windows service lifecycle
  • Loading branch information
bpetit authored Feb 12, 2024
2 parents b3b027e + 75b1de6 commit cf1b65e
Show file tree
Hide file tree
Showing 15 changed files with 921 additions and 230 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- 'CITATION'
- 'book.toml'
- 'CONTRIBUTING.md'
- '.github/workflows/exe-release-prometheuspush.yml'
pull_request:
branches: [ main, dev ]
paths-ignore:
Expand All @@ -18,6 +19,7 @@ on:
- 'CHANGELOG.md'
- 'CITATION'
- 'book.toml'
- '.github/workflows/exe-release-prometheuspush.yml'

env:
CARGO_TERM_COLOR: always
Expand Down Expand Up @@ -171,7 +173,7 @@ jobs:
rustup toolchain install stable-x86_64-pc-windows-msvc
- name: Tests
run: |
cargo test --no-default-features --features "prometheus json riemann"
cargo test --no-default-features --features "prometheus prometheuspush json riemann" exporters
- name: Build (debug mode)
run: |
cargo build --no-default-features --features "prometheus json riemann"
cargo build --no-default-features --features "prometheus prometheuspush json riemann"
23 changes: 7 additions & 16 deletions .github/workflows/exe-release-prometheuspush.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ on:
- 'book.toml'
- 'CONTRIBUTING.md'
tags: [ 'v*.*.*', 'dev*.*.*' ]
branches: [ '311-github-workflow-to-build-and-publish-a-exemsi-file-including-signed-rapl-driver-at-each-tagrelease' ]
branches: [ '336-proper-handling-of-windows-service-management' ]

env:
WRD_VERSION: v0.0.2
WRD_VERSION: v0.0.4
WRD_BASE_URL: https://github.com/hubblo-org/windows-rapl-driver/releases/download

jobs:
Expand All @@ -31,6 +31,7 @@ jobs:
& "D:\a\scaphandre\scaphandre\$dest" /verysilent /suppressmsgbox
ls "C:\Program Files (x86)\Inno Setup 6\"
- name: Get windows-rapl-driver
shell: pwsh
run: |
$dest = "DriverLoader.exe"
$url = "${{ env.WRD_BASE_URL }}/${{ env.WRD_VERSION }}/DriverLoader.exe"
Expand All @@ -44,16 +45,6 @@ jobs:
$dest = "ScaphandreDrv.inf"
$url = "${{ env.WRD_BASE_URL }}/${{ env.WRD_VERSION }}/ScaphandreDrv.inf"
Invoke-WebRequest -Uri ($url -replace '"', "") -OutFile $dest
$dest = "ScaphandreDrvTest.cer"
$url = "${{ env.WRD_BASE_URL }}/${{ env.WRD_VERSION }}/ScaphandreDrvTest.cer"
Invoke-WebRequest -Uri ($url -replace '"', "") -OutFile $dest
$dest = "devcon.exe"
$url = "${{ env.WRD_BASE_URL }}/${{ env.WRD_VERSION }}/devcon.exe"
Invoke-WebRequest -Uri ($url -replace '"', "") -OutFile $dest
$dest = "certmgr.exe"
$url = "${{ env.WRD_BASE_URL }}/${{ env.WRD_VERSION }}/certmgr.exe"
Invoke-WebRequest -Uri ($url -replace '"', "") -OutFile $dest
ls
- name: Install Rustup
uses: crazy-max/ghaction-chocolatey@v2
with:
Expand All @@ -70,14 +61,14 @@ jobs:
- name: Upload artifact #Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
run: |
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
Install-Module -Confirm:$False -Name AWS.Tools.Installer
Install-Module -Confirm:$False -Name AWS.Tools.Installer
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Import-Module AWS.Tools.Installer
Install-AWSToolsModule AWS.Tools.EC2,AWS.Tools.S3 -CleanUp -Confirm:$False
Install-AWSToolsModule AWS.Tools.EC2,AWS.Tools.S3 -CleanUp -Confirm:$False -AllowClobber
Set-AWSCredential -AccessKey ${{ secrets.S3_ACCESS_KEY_ID }} -SecretKey ${{ secrets.S3_SECRET_ACCESS_KEY }} -StoreAs default
mv packaging/windows/Output/scaphandre_installer.exe scaphandre_${GITHUB_REF_NAME}_installer.exe
mv packaging/windows/Output/scaphandre_installer.exe scaphandre_${{ github.ref_name }}_installer.exe
$clientconfig=@{
SignatureVersion="s3v4"
ServiceUrl="https://s3.fr-par.scw.cloud"
}
Write-S3Object -EndpointUrl "https://s3.fr-par.scw.cloud" -Region "fr-par" -BucketName "scaphandre" -File scaphandre_${GITHUB_REF_NAME}_installer.exe -key "x86_64/scaphandre_${GITHUB_REF_NAME}_installer.exe" -PublicReadOnly -ClientConfig $clientconfig
Write-S3Object -EndpointUrl "https://s3.fr-par.scw.cloud" -Region "fr-par" -BucketName "scaphandre" -File scaphandre_${{ github.ref_name }}_installer.exe -key "x86_64/scaphandre_${{ github.ref_name }}_installer.exe" -PublicReadOnly -ClientConfig $clientconfig
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ isahc = { version = "1.7.2", optional = true }
procfs = { version = "0.15.0" }

[target.'cfg(target_os="windows")'.dependencies]
windows = { version = "0.27.0", features = ["alloc","Win32_Storage_FileSystem","Win32_Foundation","Win32_Security","Win32_System_IO","Win32_System_Ioctl"]}
windows = { version = "0.27.0", features = ["alloc","Win32_Storage_FileSystem","Win32_Foundation","Win32_Security","Win32_System_IO","Win32_System_Ioctl","Win32_System_Threading", "Win32_System_SystemInformation"]}
windows-service = { version = "0.6.0" }
raw-cpuid = { version = "10.5.0" }
core_affinity = { version = "0.8.1"}
x86 = { version = "0.52.0" }

[features]
default = ["prometheus", "riemann", "warpten", "json", "containers", "prometheuspush"]
Expand Down
14 changes: 7 additions & 7 deletions packaging/windows/dev_installer.iss
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ Name: "english"; MessagesFile: "compiler:Default.isl"
[Files]
Source: "{#MyAppSourceFolder}\target\release\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#RaplDriverSourceFolder}\x64\Release\DriverLoader.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#RaplDriverSourceFolder}\ScaphandreDrv\ScaphandreDrv.inf"; DestDir: "{app}"; Flags: ignoreversion
; Source: "{#RaplDriverSourceFolder}\ScaphandreDrv\ScaphandreDrv.sys"; DestDir: "{#SystemFolder}";
; Source: "{#RaplDriverSourceFolder}\ScaphandreDrv\ScaphandreDrv.sys"; DestDir: "{#System64Folder}";
Source: "{#RaplDriverSourceFolder}\ScaphandreDrv\ScaphandreDrv.sys"; DestDir: "{app}";
Source: "{#RaplDriverSourceFolder}\ScaphandreDrv\ScaphandreDrv.cat"; DestDir: "{app}";
; Source: "{#RaplDriverSourceFolder}\ScaphandreDrv\ScaphandreDrv.cat"; DestDir: "{#SystemFolder}";
; Source: "{#RaplDriverSourceFolder}\ScaphandreDrv\ScaphandreDrv.cat"; DestDir: "{#System64Folder}";
Source: "{#RaplDriverSourceFolder}\x64\Release\ScaphandreDrv\ScaphandreDrv.inf"; DestDir: "{app}"; Flags: ignoreversion
; Source: "{#RaplDriverSourceFolder}\x64\Release\ScaphandreDrv\ScaphandreDrv.sys"; DestDir: "{#SystemFolder}";
; Source: "{#RaplDriverSourceFolder}\x64\Release\ScaphandreDrv\ScaphandreDrv.sys"; DestDir: "{#System64Folder}";
Source: "{#RaplDriverSourceFolder}\x64\Release\ScaphandreDrv\ScaphandreDrv.sys"; DestDir: "{app}";
Source: "{#RaplDriverSourceFolder}\x64\Release\ScaphandreDrv\ScaphandreDrv.cat"; DestDir: "{app}";
; Source: "{#RaplDriverSourceFolder}\x64\Release\ScaphandreDrv\ScaphandreDrv.cat"; DestDir: "{#SystemFolder}";
; Source: "{#RaplDriverSourceFolder}\x64\Release\ScaphandreDrv\ScaphandreDrv.cat"; DestDir: "{#System64Folder}";
Source: "C:\Program Files (x86)\Windows Kits\10\Tools\10.0.22621.0\x64\devcon.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\certmgr.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#MyAppSourceFolder}\README.md"; DestDir: "{app}"; Flags: ignoreversion
Expand Down
5 changes: 2 additions & 3 deletions packaging/windows/installer.iss
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ Source: "../../ScaphandreDrv.sys"; DestDir: "{app}";
Source: "../../ScaphandreDrv.cat"; DestDir: "{app}";
; Source: "../../ScaphandreDrv.cat"; DestDir: "{#SystemFolder}";
; Source: "../../ScaphandreDrv.cat"; DestDir: "{#System64Folder}";
Source: "../../devcon.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "../../certmgr.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Windows Kits\10\Tools\10.0.22621.0\x64\devcon.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\certmgr.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "../../README.md"; DestDir: "{app}"; Flags: ignoreversion
Source: "../../CHANGELOG.md"; DestDir: "{app}"; Flags: ignoreversion
Source: "../../ScaphandreDrvTest.cer"; DestDir: "{app}"; Flags: ignoreversion
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Expand Down
40 changes: 40 additions & 0 deletions packaging/windows/register_log_source.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# https://github.com/dansmith
#
$source = "scaphandre"


$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)

if($IsAdmin -eq $false)
{
[System.Reflection.Assembly]::LoadWithPartialName(System.Windows.Forms)
[Windows.Forms.MessageBox]::Show(Please run this as an Administrator,
Not Administrator,
[Windows.Forms.MessageBoxButtons]::OK,
[Windows.Forms.MessageBoxIcon]::Information)
exit
}


if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false)
{
[System.Diagnostics.EventLog]::CreateEventSource($source, "Application")

[System.Reflection.Assembly]::LoadWithPartialName(System.Windows.Forms)
[Windows.Forms.MessageBox]::Show(Event log created successfully,
Complete,
[Windows.Forms.MessageBoxButtons]::OK,
[Windows.Forms.MessageBoxIcon]::Information)
}
else
{
[System.Reflection.Assembly]::LoadWithPartialName(System.Windows.Forms)
[Windows.Forms.MessageBox]::Show(Event log already exists,
Complete,
[Windows.Forms.MessageBoxButtons]::OK,
[Windows.Forms.MessageBoxIcon]::Information)

}
21 changes: 2 additions & 19 deletions src/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,23 +630,6 @@ impl MetricGenerator {
description: String::from("Total swap space on the host, in bytes."),
metric_value: MetricValueType::Text(metric_value.value),
});

if let Some(psys) = self.topology.get_rapl_psys_energy_microjoules() {
self.data.push(Metric {
name: String::from("scaph_host_rapl_psys_microjoules"),
metric_type: String::from("counter"),
ttl: 60.0,
timestamp: psys.timestamp,
hostname: self.hostname.clone(),
state: String::from("ok"),
tags: vec!["scaphandre".to_string()],
attributes: HashMap::new(),
description: String::from(
"Raw extract of RAPL PSYS domain energy value, in microjoules",
),
metric_value: MetricValueType::Text(psys.value),
})
}
}

/// Generate socket metrics.
Expand Down Expand Up @@ -887,7 +870,7 @@ impl MetricGenerator {

/// Generate process metrics.
fn gen_process_metrics(&mut self) {
debug!("In gen_process_metrics.");
trace!("In gen_process_metrics.");
#[cfg(feature = "containers")]
if self.watch_containers {
let now = current_system_time_since_epoch().as_secs().to_string();
Expand Down Expand Up @@ -1029,7 +1012,7 @@ impl MetricGenerator {
Utc::now().format("%Y-%m-%dT%H:%M:%S")
);
self.gen_process_metrics();
debug!("self_metrics: {:#?}", self.data);
trace!("self_metrics: {:#?}", self.data);
}

pub fn pop_metrics(&mut self) -> Vec<Metric> {
Expand Down
2 changes: 1 addition & 1 deletion src/exporters/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//! [scrape](https://prometheus.io/docs/prometheus/latest/getting_started).

use super::utils;
use crate::current_system_time_since_epoch;
use crate::exporters::{Exporter, MetricGenerator, MetricValueType};
use crate::sensors::utils::current_system_time_since_epoch;
use crate::sensors::{Sensor, Topology};
use chrono::Utc;
use hyper::service::{make_service_fn, service_fn};
Expand Down
16 changes: 13 additions & 3 deletions src/exporters/stdout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ impl StdoutExporter {
fn summarized_view(&mut self, metrics: Vec<Metric>) {
let mut metrics_iter = metrics.iter();
let none_value = MetricValueType::Text("0".to_string());
let mut host_power_source = String::from("");
let host_power = match metrics_iter.find(|x| x.name == "scaph_host_power_microwatts") {
Some(m) => &m.metric_value,
Some(m) => {
if let Some(src) = &m.attributes.get("value_source") {
host_power_source = src.to_string()
}
&m.metric_value
}
None => &none_value,
};

Expand All @@ -121,8 +127,9 @@ impl StdoutExporter {
}

println!(
"Host:\t{} W",
(format!("{host_power}").parse::<f64>().unwrap() / 1000000.0)
"Host:\t{} W from {}",
(format!("{host_power}").parse::<f64>().unwrap() / 1000000.0),
host_power_source
);

if domain_names.is_some() {
Expand All @@ -133,6 +140,7 @@ impl StdoutExporter {
.iter()
.filter(|x| x.name == "scaph_socket_power_microwatts")
{
debug!("✅ Found socket power metric !");
let power = format!("{}", s.metric_value).parse::<f32>().unwrap() / 1000000.0;
let mut power_str = String::from("----");
if power > 0.0 {
Expand Down Expand Up @@ -176,6 +184,8 @@ impl StdoutExporter {
}
}
println!("{to_print}\n");
} else {
println!("{to_print} Could'nt get per-domain metrics.\n");
}
}

Expand Down
8 changes: 0 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use sensors::msr_rapl;
#[cfg(target_os = "linux")]
use sensors::powercap_rapl;

use std::time::{Duration, SystemTime};

/// Create a new [`Sensor`] instance with the default sensor available,
/// with its default options.
pub fn get_default_sensor() -> impl sensors::Sensor {
Expand All @@ -30,12 +28,6 @@ pub fn get_default_sensor() -> impl sensors::Sensor {
return msr_rapl::MsrRAPLSensor::new();
}

fn current_system_time_since_epoch() -> Duration {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
}

// Copyright 2020 The scaphandre authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
Loading

0 comments on commit cf1b65e

Please sign in to comment.