1- use anyhow:: { Result , bail} ;
21use async_trait:: async_trait;
32use futures:: { FutureExt , future:: join_all} ;
43use log:: debug;
5- use std:: process:: Command ;
64
75use crate :: helpers:: {
86 CheckGroup , CheckGroupCategory , CheckGroupResult , CheckResult ,
97 CheckResultValue :: { Errored , Failed , Passed } ,
108 host_executor:: HostNamespaceExecutor ,
9+ services as svchelpers,
1110} ;
1211
1312const GROUP_IDENTIFIER : & str = "services" ;
1413const NAME : & str = "Service Status Checks" ;
1514
16- #[ derive( Debug , Clone , Copy , PartialEq ) ]
17- enum InitSystem {
18- Systemd ,
19- OpenRC ,
20- Unknown ,
21- }
22-
2315pub struct ServiceChecks {
2416 host_executor : HostNamespaceExecutor ,
2517}
@@ -61,10 +53,10 @@ impl ServiceChecks {
6153 async fn check_daemon ( & self ) -> CheckResult {
6254 let name = "Protect daemon status" ;
6355 let sname = "protect-daemon" ;
64- let init = self . detect_init_system ( ) . await ;
56+ let init = svchelpers :: detect_init_system ( & self . host_executor ) . await ;
6557 debug ! ( "detected init system: {:?}\n " , init) ;
6658
67- match self . is_running ( sname. into ( ) , init) . await {
59+ match svchelpers :: is_running ( & self . host_executor , sname. into ( ) , init) . await {
6860 Ok ( true ) => CheckResult :: new ( name, Passed ) ,
6961 Ok ( false ) => CheckResult :: new ( name, Failed ( format ! ( "{} not running" , & sname) ) ) ,
7062 Err ( e) => CheckResult :: new (
@@ -77,10 +69,10 @@ impl ServiceChecks {
7769 async fn check_storage ( & self ) -> CheckResult {
7870 let name = "Protect storage daemon status" ;
7971 let sname = "protect-storage" ;
80- let init = self . detect_init_system ( ) . await ;
72+ let init = svchelpers :: detect_init_system ( & self . host_executor ) . await ;
8173 debug ! ( "detected init system: {:?}\n " , init) ;
8274
83- match self . is_running ( sname. into ( ) , init) . await {
75+ match svchelpers :: is_running ( & self . host_executor , sname. into ( ) , init) . await {
8476 Ok ( true ) => CheckResult :: new ( name, Passed ) ,
8577 Ok ( false ) => CheckResult :: new ( name, Failed ( format ! ( "{} not running" , & sname) ) ) ,
8678 Err ( e) => CheckResult :: new (
@@ -93,10 +85,10 @@ impl ServiceChecks {
9385 async fn check_network ( & self ) -> CheckResult {
9486 let name = "Protect network daemon status" ;
9587 let sname = "protect-network" ;
96- let init = self . detect_init_system ( ) . await ;
88+ let init = svchelpers :: detect_init_system ( & self . host_executor ) . await ;
9789 debug ! ( "detected init system: {:?}\n " , init) ;
9890
99- match self . is_running ( sname. into ( ) , init) . await {
91+ match svchelpers :: is_running ( & self . host_executor , sname. into ( ) , init) . await {
10092 Ok ( true ) => CheckResult :: new ( name, Passed ) ,
10193 Ok ( false ) => CheckResult :: new ( name, Failed ( format ! ( "{} not running" , & sname) ) ) ,
10294 Err ( e) => CheckResult :: new (
@@ -105,54 +97,6 @@ impl ServiceChecks {
10597 ) ,
10698 }
10799 }
108-
109- async fn detect_init_system ( & self ) -> InitSystem {
110- self . host_executor
111- . spawn_in_host_ns ( async move {
112- // Check if systemd is PID 1
113- if let Ok ( output) = Command :: new ( "ps" ) . args ( [ "-p" , "1" , "-o" , "comm=" ] ) . output ( ) {
114- let comm = String :: from_utf8_lossy ( & output. stdout ) ;
115- if comm. trim ( ) == "systemd" {
116- return InitSystem :: Systemd ;
117- }
118- }
119-
120- // otherwise, check if rc-service exists (OpenRC)
121- if Command :: new ( "which" )
122- . arg ( "rc-service" )
123- . output ( )
124- . map ( |o| o. status . success ( ) )
125- . unwrap_or ( false )
126- {
127- return InitSystem :: OpenRC ;
128- }
129- InitSystem :: Unknown
130- } )
131- . await
132- . unwrap_or ( InitSystem :: Unknown )
133- }
134-
135- async fn is_running ( & self , service : String , init : InitSystem ) -> Result < bool > {
136- self . host_executor
137- . spawn_in_host_ns ( async move {
138- match init {
139- InitSystem :: Systemd => {
140- let status = Command :: new ( "systemctl" )
141- . args ( [ "is-active" , "--quiet" , & service] )
142- . status ( ) ?;
143- Ok ( status. success ( ) )
144- }
145- InitSystem :: OpenRC => {
146- let status = Command :: new ( "rc-service" )
147- . args ( [ & service, "status" ] )
148- . status ( ) ?;
149- Ok ( status. success ( ) )
150- }
151- InitSystem :: Unknown => bail ! ( "unknown init system" ) ,
152- }
153- } )
154- . await ?
155- }
156100}
157101
158102#[ async_trait]
0 commit comments