@@ -9,7 +9,7 @@ use ipmi_rs::connection::{IpmiConnection, LogicalUnit, Message, NetFn, Request};
9
9
mod common;
10
10
11
11
#[ derive( Parser ) ]
12
- pub struct Command {
12
+ struct Command {
13
13
#[ clap( flatten) ]
14
14
common : CommonOpts ,
15
15
}
@@ -34,8 +34,19 @@ fn get_message() -> std::io::Result<Message> {
34
34
) )
35
35
}
36
36
37
+ pub struct PowerConsumption {
38
+ grp_id : u8 , /* first byte: Group Extension ID */
39
+ curr_pwr : u16 ,
40
+ min_sample : u16 ,
41
+ max_sample : u16 ,
42
+ avg_pwr : u16 ,
43
+ time_stamp : u32 , /* time since epoch */
44
+ sample : u32 ,
45
+ state : u8 ,
46
+ }
47
+
37
48
// See: https://github.com/ipmitool/ipmitool/blob/IPMITOOL_1_8_19/lib/ipmi_dcmi.c#L1398-L1454
38
- fn ipmi_dcmi_pwr_rd ( ipmi : IpmiConnectionEnum ) -> Result < ( ) , Error > {
49
+ pub fn ipmi_dcmi_pwr_rd ( ipmi : IpmiConnectionEnum ) -> Result < PowerConsumption , Error > {
39
50
let message = get_message ( ) ?;
40
51
41
52
let mut request: Request = Request :: new ( message, LogicalUnit :: Zero ) ;
@@ -63,53 +74,64 @@ fn ipmi_dcmi_pwr_rd(ipmi: IpmiConnectionEnum) -> Result<(), Error> {
63
74
state: u8 ,
64
75
} ) ;
65
76
let view = power_data:: View :: new ( response_data) ;
66
- // let grp_id: u8 = view.grp_id().read();
77
+ let grp_id: u8 = view. grp_id ( ) . read ( ) ;
67
78
let curr_pwr: u16 = view. curr_pwr ( ) . read ( ) ;
68
79
let min_sample: u16 = view. min_sample ( ) . read ( ) ;
69
80
let max_sample: u16 = view. max_sample ( ) . read ( ) ;
70
81
let avg_pwr: u16 = view. avg_pwr ( ) . read ( ) ;
71
82
let time_stamp: u32 = view. time_stamp ( ) . read ( ) ;
72
- let date_time = Utc . timestamp_opt ( time_stamp as i64 , 0 ) . unwrap ( ) ;
73
83
let sample: u32 = view. sample ( ) . read ( ) ;
74
84
let state: u8 = view. state ( ) . read ( ) ;
85
+ Ok ( PowerConsumption {
86
+ grp_id,
87
+ curr_pwr,
88
+ min_sample,
89
+ max_sample,
90
+ avg_pwr,
91
+ time_stamp,
92
+ sample,
93
+ state,
94
+ } )
95
+ }
75
96
97
+ pub fn ipmi_dcmi_pwr_format_text ( pwr : PowerConsumption ) {
98
+ let date_time = Utc . timestamp_opt ( pwr. time_stamp as i64 , 0 ) . unwrap ( ) ;
76
99
//println!("grp_id: {}:{:02X?}", grp_id, grp_id);
77
100
println ! ( "" ) ;
78
101
println ! (
79
102
" Instantaneous power reading : {:<8} Watts" ,
80
- curr_pwr
103
+ pwr . curr_pwr
81
104
) ;
82
105
println ! (
83
106
" Minimum during sampling period : {:<8} Watts" ,
84
- min_sample
107
+ pwr . min_sample
85
108
) ;
86
109
println ! (
87
110
" Maximum during sampling period : {:<8} Watts" ,
88
- max_sample
111
+ pwr . max_sample
89
112
) ;
90
113
91
114
println ! (
92
115
" Average power reading over sample period : {:<8} Watts" ,
93
- avg_pwr
116
+ pwr . avg_pwr
94
117
) ;
95
118
println ! (
96
119
" IPMI timestamp : {}" ,
97
120
date_time
98
121
) ;
99
122
println ! (
100
123
" Sampling period : {} Milliseconds" ,
101
- sample
124
+ pwr . sample
102
125
) ;
103
126
println ! (
104
127
" Power reading state is : {}" ,
105
- match state {
128
+ match pwr . state {
106
129
0x40 => "activated" ,
107
130
_ => "deactivated" ,
108
131
}
109
132
) ;
110
133
println ! ( "" ) ;
111
134
println ! ( "" ) ;
112
- Ok ( ( ) )
113
135
}
114
136
115
137
fn main ( ) -> std:: io:: Result < ( ) > {
@@ -119,5 +141,8 @@ fn main() -> std::io::Result<()> {
119
141
120
142
let command = Command :: parse ( ) ;
121
143
let ipmi = command. common . get_connection ( ) ?;
122
- ipmi_dcmi_pwr_rd ( ipmi)
144
+ match ipmi_dcmi_pwr_rd ( ipmi) {
145
+ Ok ( data) => Ok ( ipmi_dcmi_pwr_format_text ( data) ) ,
146
+ Err ( err) => Err ( err) ,
147
+ }
123
148
}
0 commit comments