@@ -19,6 +19,7 @@ import (
1919 "encoding/json"
2020 "flag"
2121 "fmt"
22+ "math"
2223 "net"
2324 "os"
2425 "strconv"
@@ -36,6 +37,11 @@ import (
3637 terminal "golang.org/x/term"
3738)
3839
40+ const (
41+ CARADNO_NODE_BINARY = "cardano-node"
42+ AMARU_BINARY = "amaru"
43+ )
44+
3945// Global command line flags
4046var cmdlineFlags struct {
4147 configFile string
@@ -1180,6 +1186,48 @@ func getResourceText(ctx context.Context) string {
11801186
11811187func getProcessMetrics (ctx context.Context ) (* process.Process , error ) {
11821188 cfg := config .GetConfig ()
1189+
1190+ if cfg .Node .Binary == AMARU_BINARY {
1191+ return getProcessMetricsByPidFile (cfg , ctx )
1192+ } else {
1193+ return getProcessMetricsByNameAndPort (cfg , ctx )
1194+ }
1195+ }
1196+
1197+ func getProcessMetricsByPidFile (cfg * config.Config , ctx context.Context ) (* process.Process , error ) {
1198+ data , err := os .ReadFile (cfg .Node .PidFile )
1199+ if err != nil {
1200+ return nil , fmt .Errorf ("failed to read pid file: %w" , err )
1201+ }
1202+ pidStr := strings .TrimSpace (string (data ))
1203+ pid , err := strconv .Atoi (pidStr )
1204+ if err != nil {
1205+ return nil , fmt .Errorf ("invalid pid in pid file: %w" , err )
1206+ }
1207+ if pid <= 0 || pid > math .MaxInt32 {
1208+ return nil , fmt .Errorf ("invalid pid %d: out of int32 range" , pid )
1209+ }
1210+
1211+ // the overflow is checked above
1212+ //nolint:gosec
1213+ proc , err := process .NewProcessWithContext (ctx , int32 (pid ))
1214+ if err != nil {
1215+ return nil , fmt .Errorf ("failed to get process %d: %w" , pid , err )
1216+ }
1217+
1218+ exists , err := proc .IsRunning ()
1219+ if err != nil {
1220+ return nil , fmt .Errorf ("failed to check if process %d is running: %w" , pid , err )
1221+ }
1222+
1223+ if ! exists {
1224+ return nil , fmt .Errorf ("process %d is not running" , pid )
1225+ }
1226+
1227+ return proc , nil
1228+ }
1229+
1230+ func getProcessMetricsByNameAndPort (cfg * config.Config , ctx context.Context ) (* process.Process , error ) {
11831231 r , _ := process .NewProcessWithContext (ctx , 0 )
11841232 processes , err := process .ProcessesWithContext (ctx )
11851233 if err != nil {
@@ -1188,17 +1236,20 @@ func getProcessMetrics(ctx context.Context) (*process.Process, error) {
11881236 for _ , p := range processes {
11891237 n , err := p .NameWithContext (ctx )
11901238 if err != nil {
1191- return r , fmt . Errorf ( "failed to get process name: %w" , err )
1239+ continue
11921240 }
1241+
11931242 c , err := p .CmdlineWithContext (ctx )
11941243 if err != nil {
1195- return r , fmt . Errorf ( "failed to get process cmdline: %w" , err )
1244+ continue
11961245 }
1246+
11971247 if strings .Contains (n , cfg .Node .Binary ) &&
11981248 strings .Contains (c , strconv .FormatUint (uint64 (cfg .Node .Port ), 10 )) {
11991249 r = p
12001250 }
12011251 }
1252+
12021253 return r , nil
12031254}
12041255
0 commit comments