@@ -7,7 +7,6 @@ use crate::{
77use anyhow:: { Context , Result , bail} ;
88use azure_iot_sdk:: client:: { IotHubClient , IotMessage } ;
99use inotify:: WatchMask ;
10- use lazy_static:: lazy_static;
1110use log:: { info, warn} ;
1211use serde:: { Deserialize , Serialize , Serializer , ser:: Error } ;
1312use serde_json:: json;
@@ -22,17 +21,10 @@ use time::format_description::well_known::Rfc3339;
2221use tokio:: sync:: mpsc;
2322
2423static BOOTLOADER_UPDATED_FILE : & str = "/run/omnect-device-service/omnect_bootloader_updated" ;
25-
26- // ToDo
27- lazy_static ! {
28- static ref REFRESH_SYSTEM_INFO_INTERVAL_SECS : u64 = {
29- const REFRESH_SYSTEM_INFO_INTERVAL_SECS_DEFAULT : & str = "60" ;
30- env:: var( "REFRESH_SYSTEM_INFO_INTERVAL_SECS" )
31- . unwrap_or( REFRESH_SYSTEM_INFO_INTERVAL_SECS_DEFAULT . to_string( ) )
32- . parse:: <u64 >( )
33- . expect( "cannot parse REFRESH_SYSTEM_INFO_INTERVAL_SECS env var" )
34- } ;
35- }
24+ #[ cfg( not( feature = "mock" ) ) ]
25+ static TIMESYNC_FILE : & str = "/run/systemd/timesync/synchronized" ;
26+ #[ cfg( feature = "mock" ) ]
27+ static TIMESYNC_FILE : & str = "/tmp/synchronized" ;
3628
3729#[ derive( Default , Serialize ) ]
3830struct Label {
@@ -73,7 +65,7 @@ enum MetricValue {
7365}
7466
7567impl MetricValue {
76- fn to_metric ( self ) -> Metric {
68+ fn metric ( self ) -> Metric {
7769 let ( name, value, labels) = match self {
7870 MetricValue :: CpuUsage ( value) => ( "cpu_usage" . to_owned ( ) , value, Label :: default ( ) ) ,
7971 MetricValue :: MemoryUsed ( value) => ( "memory_used" . to_owned ( ) , value, Label :: default ( ) ) ,
@@ -130,15 +122,6 @@ where
130122 s. serialize_str ( & time_stamp)
131123}
132124
133- // ToDo rm lazy_staticw
134- lazy_static ! {
135- static ref TIMESYNC_FILE : & ' static Path = if cfg!( feature = "mock" ) {
136- Path :: new( "/tmp/synchronized" )
137- } else {
138- Path :: new( "/run/systemd/timesync/synchronized" )
139- } ;
140- }
141-
142125#[ derive( Clone , Debug , Deserialize , PartialEq ) ]
143126pub struct FleetIdCommand {
144127 pub fleet_id : String ,
@@ -253,12 +236,18 @@ impl SystemInfo {
253236 RootPartition :: current( ) ?. as_str( )
254237 ) ;
255238
256- feature:: add_watch :: < Self > ( & TIMESYNC_FILE , WatchMask :: CREATE | WatchMask :: ONESHOT ) ?;
239+ feature:: add_watch :: < Self > (
240+ Path :: new ( TIMESYNC_FILE ) ,
241+ WatchMask :: CREATE | WatchMask :: ONESHOT ,
242+ ) ?;
257243
258- if 0 < * REFRESH_SYSTEM_INFO_INTERVAL_SECS {
259- feature:: notify_interval :: < Self > ( Duration :: from_secs (
260- * REFRESH_SYSTEM_INFO_INTERVAL_SECS ,
261- ) ) ?;
244+ let refresh_interval = env:: var ( "REFRESH_SYSTEM_INFO_INTERVAL_SECS" )
245+ . unwrap_or ( "60" . to_string ( ) )
246+ . parse :: < u64 > ( )
247+ . context ( "cannot parse REFRESH_SYSTEM_INFO_INTERVAL_SECS env var" ) ?;
248+
249+ if 0 < refresh_interval {
250+ feature:: notify_interval :: < Self > ( Duration :: from_secs ( refresh_interval) ) ?;
262251 }
263252
264253 Ok ( SystemInfo {
@@ -332,12 +321,14 @@ impl SystemInfo {
332321 return Ok ( ( ) ) ;
333322 } ;
334323
335- let Ok ( mut time_stamp) = TIME_STAMP . write ( ) else {
336- bail ! ( "metrics: failed to lock TIME_STAMP" )
337- } ;
338- * time_stamp = time:: OffsetDateTime :: now_utc ( )
339- . format ( & Rfc3339 )
340- . context ( "metrics: failed to get and format timestamp" ) ?;
324+ {
325+ let Ok ( mut time_stamp) = TIME_STAMP . write ( ) else {
326+ bail ! ( "metrics: failed to lock TIME_STAMP" )
327+ } ;
328+ * time_stamp = time:: OffsetDateTime :: now_utc ( )
329+ . format ( & Rfc3339 )
330+ . context ( "metrics: failed to get and format timestamp" ) ?;
331+ }
341332
342333 self . hardware_info . components . refresh ( true ) ;
343334 self . hardware_info . system . refresh_cpu_usage ( ) ;
@@ -356,16 +347,16 @@ impl SystemInfo {
356347 . unwrap_or ( ( 0 , 0 ) ) ;
357348
358349 let mut metrics = vec ! [
359- MetricValue :: CpuUsage ( self . hardware_info. system. global_cpu_usage( ) as f64 ) . to_metric ( ) ,
360- MetricValue :: MemoryUsed ( self . hardware_info. system. used_memory( ) as f64 ) . to_metric ( ) ,
361- MetricValue :: MemoryTotal ( self . hardware_info. system. total_memory( ) as f64 ) . to_metric ( ) ,
362- MetricValue :: DiskUsed ( disk_used as f64 ) . to_metric ( ) ,
363- MetricValue :: DiskTotal ( disk_total as f64 ) . to_metric ( ) ,
350+ MetricValue :: CpuUsage ( self . hardware_info. system. global_cpu_usage( ) as f64 ) . metric ( ) ,
351+ MetricValue :: MemoryUsed ( self . hardware_info. system. used_memory( ) as f64 ) . metric ( ) ,
352+ MetricValue :: MemoryTotal ( self . hardware_info. system. total_memory( ) as f64 ) . metric ( ) ,
353+ MetricValue :: DiskUsed ( disk_used as f64 ) . metric ( ) ,
354+ MetricValue :: DiskTotal ( disk_total as f64 ) . metric ( ) ,
364355 ] ;
365356
366357 self . hardware_info . components . iter ( ) . for_each ( |c| {
367358 if let Some ( t) = c. temperature ( ) {
368- metrics. push ( MetricValue :: Temp ( t. into ( ) , c. label ( ) . to_string ( ) ) . to_metric ( ) )
359+ metrics. push ( MetricValue :: Temp ( t. into ( ) , c. label ( ) . to_string ( ) ) . metric ( ) )
369360 } ;
370361 } ) ;
371362
0 commit comments