1- //! Module to define behavior of sys info collection.
1+ // src/collector/nvme.rs
2+ //! NVMe SMART collection via linux_nvme_sys.
3+
24use linux_nvme_sys:: { nvme_admin_cmd, nvme_admin_opcode:: nvme_admin_get_log_page, nvme_smart_log} ;
35use serde:: Serialize ;
4- use serde_json:: { Value , json} ;
5- use std:: fs;
6- use std:: fs:: OpenOptions ;
6+ use std:: fs:: { self , OpenOptions } ;
77use std:: io;
88use std:: mem:: { size_of, zeroed} ;
99use std:: os:: unix:: io:: AsRawFd ;
10- use std:: process:: Command ;
11- use sysinfo:: { Disks , Networks , System } ;
1210
1311// Serialize smart log metrics
1412#[ derive( Debug , Serialize ) ]
@@ -40,140 +38,17 @@ pub struct NvmesSmartLog {
4038 pub warning_temp_time : Option < u64 > ,
4139}
4240
43- /// Helper function to convert a 16-byte little-endian NVMe counter into u64
41+ /// Function to convert a 16-byte little-endian NVMe counter into u64.
4442fn le_16_to_u128 ( bytes : & [ u8 ; 16 ] ) -> u128 {
4543 u128:: from_le_bytes ( * bytes)
4644}
4745
48- /// Helper function to convert a 32-bit little-endian NVMe counter into u64
46+ /// Function to convert a 32-bit little-endian NVMe counter into u64.
4947fn le32_to_u64 ( v : linux_nvme_sys:: __le32 ) -> u64 {
5048 u32:: from ( v) as u64
5149}
5250
53- /// Function to get raw uptime.
54- fn uptime_raw ( _sys : & System ) -> String {
55- System :: uptime ( ) . to_string ( )
56- }
57-
58- /// Function to get raw cpufreq.
59- fn cpu_freq_raw ( sys : & System ) -> String {
60- let cpu_freq = sys. cpus ( ) . first ( ) . map ( |cpu| cpu. frequency ( ) ) . unwrap_or ( 0 ) ;
61- cpu_freq. to_string ( )
62- }
63-
64- /// Function to get timestamp
65- pub fn get_timestamp ( ) -> u64 {
66- std:: time:: SystemTime :: now ( )
67- . duration_since ( std:: time:: UNIX_EPOCH )
68- . unwrap ( )
69- . as_secs ( )
70- }
71-
72- /// Function to get hostname
73- pub fn get_hostname ( _sys : & System ) -> String {
74- System :: host_name ( )
75- . unwrap_or_else ( || "unknown" . to_string ( ) )
76- . replace ( '"' , "\\ \" " )
77- }
78-
79- /// Function to collect system metrics as single json object.
80- pub fn get_sysinfo ( sys : & System ) -> Value {
81- json ! ( {
82- "timestamp" : get_timestamp( ) ,
83- "hostname" : get_hostname( sys) ,
84- "uptime" : uptime_raw( sys) ,
85- "cpu_freq_mhz" : cpu_freq_raw( sys) ,
86- "disk_usage" : get_disk_usage( ) ,
87- "network" : get_if_data( ) ,
88- "smart_log" : collect_smart_log( ) ,
89- } )
90- }
91-
92- /// Function to get JSON formatted uptime.
93- pub fn uptime_json ( sys : & System ) -> Value {
94- json ! ( { "uptime" : uptime_raw( sys) } )
95- }
96-
97- /// Function to get JSON formatted cpufreq.
98- pub fn cpu_freq_json ( sys : & System ) -> Value {
99- json ! ( { "cpu_freq_mhz" : cpu_freq_raw( sys) } )
100- }
101-
102- /// Function to get metrics from interfaces.
103- pub fn get_if_data ( ) -> Vec < Value > {
104- let networks = Networks :: new_with_refreshed_list ( ) ;
105-
106- networks
107- . iter ( )
108- . map ( |( name, data) | {
109- json ! ( {
110- "interface" : name. replace( '"' , "\\ \" " ) ,
111- "rx_bytes" : data. total_received( ) ,
112- "tx_bytes" : data. total_transmitted( )
113- } )
114- } )
115- . collect ( )
116- }
117-
118- /// Function to get disk usage information.
119- pub fn get_disk_usage ( ) -> Vec < Value > {
120- let disks = Disks :: new_with_refreshed_list ( ) ;
121-
122- disks
123- . iter ( )
124- . map ( |disk| {
125- let total = disk. total_space ( ) ;
126- let available = disk. available_space ( ) ;
127- let used = total - available;
128- let used_percent = if total > 0 {
129- ( used as f64 / total as f64 ) * 100.0
130- } else {
131- 0.0
132- } ;
133-
134- json ! ( {
135- "mount" : disk. mount_point( ) . to_string_lossy( ) . replace( '"' , "\\ \" " ) ,
136- "total_gb" : total / 1_000_000_000 ,
137- "used_gb" : used / 1_000_000_000 ,
138- "used_percent" : used_percent
139- } )
140- } )
141- . collect ( )
142- }
143-
144- /// Function to get status of specific systemd services by name
145- pub fn get_service_status ( services : & [ String ] ) -> Vec < Value > {
146- let mut results = Vec :: new ( ) ;
147-
148- for service in services {
149- let status = get_service_active_status ( & service) ;
150-
151- let service_status = json ! ( {
152- "service_name" : service,
153- "status" : status
154- } ) ;
155-
156- results. push ( service_status) ;
157- }
158-
159- results
160- }
161-
162- /// Get the active status of a service
163- fn get_service_active_status ( service : & str ) -> String {
164- match Command :: new ( "systemctl" )
165- . args ( & [ "is-active" , service] )
166- . output ( )
167- {
168- Ok ( output) => str:: from_utf8 ( & output. stdout )
169- . unwrap_or ( "unknown" )
170- . trim ( )
171- . to_string ( ) ,
172- Err ( _) => "error" . to_string ( ) ,
173- }
174- }
175-
176- /// Function to discover controllers exposed on the server
51+ /// Function to discover controllers exposed on the server.
17752pub fn list_nvme_controllers ( ) -> Vec < String > {
17853 let mut names = Vec :: new ( ) ;
17954
@@ -299,7 +174,7 @@ pub fn list_nvme_controllers() -> Vec<String> {
299174// results
300175// }
301176
302- /// Function to extract smart log through linux-nvme-sys crate
177+ /// Function to extract smart log through linux-nvme-sys crate.
303178fn smart_log_from_kernel ( nvme_name : String , raw : & nvme_smart_log ) -> NvmesSmartLog {
304179 // temp is 2 bytes, just join as u16?
305180 let temp = u16:: from_le_bytes ( [ raw. temperature [ 0 ] , raw. temperature [ 1 ] ] ) as u64 ;
@@ -349,7 +224,7 @@ fn smart_log_from_kernel(nvme_name: String, raw: &nvme_smart_log) -> NvmesSmartL
349224 }
350225}
351226
352- /// Get the raw nvme_smart_log from a controller device, e.g. "/dev/nvme0"
227+ /// Function to extract raw nvme_smart_log from a controller.
353228pub fn get_nvme_smart_log_raw ( dev_path : & str ) -> io:: Result < nvme_smart_log > {
354229 let file = OpenOptions :: new ( )
355230 . read ( true )
@@ -394,7 +269,7 @@ pub fn get_nvme_smart_log_raw(dev_path: &str) -> io::Result<nvme_smart_log> {
394269 }
395270}
396271
397- /// Function to collect extracted smart log data
272+ /// Function to collect extracted smart log data.
398273pub fn collect_smart_log ( ) -> Vec < NvmesSmartLog > {
399274 let mut results = Vec :: new ( ) ;
400275 let ctrls = list_nvme_controllers ( ) ;
@@ -412,6 +287,5 @@ pub fn collect_smart_log() -> Vec<NvmesSmartLog> {
412287 }
413288 }
414289 }
415-
416290 results
417291}
0 commit comments