2022-12-21 16:12:15 +00:00
package sysmon.plugins.power ;
2021-05-04 10:08:24 +00:00
2021-06-08 18:24:43 +00:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2021-06-10 06:55:03 +00:00
import java.io.BufferedReader ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.InputStreamReader ;
2022-02-25 14:45:11 +00:00
import java.util.Arrays ;
2021-09-10 19:58:14 +00:00
import java.util.Objects ;
2022-11-07 12:33:35 +00:00
import java.util.TreeMap ;
2021-05-10 14:56:56 +00:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2022-12-21 16:12:15 +00:00
public class PowerProcessorStat {
2021-05-04 10:08:24 +00:00
2022-12-21 16:12:15 +00:00
private static final Logger log = LoggerFactory . getLogger ( PowerProcessorStat . class ) ;
2021-06-08 18:24:43 +00:00
2021-05-11 13:37:23 +00:00
// System configuration: type=Shared mode=Uncapped smt=8 lcpu=8 mem=4096MB psize=19 ent=0.50
2021-09-14 10:46:21 +00:00
private static final Pattern patternAixShared = Pattern . compile ( " ^System configuration: type=( \\ S+) mode=( \\ S+) smt=( \\ d+) lcpu=( \\ d+) mem=( \\ d+)MB psize=( \\ d+) ent=( \\ d+ \\ .? \\ d*) " ) ;
2021-06-08 18:24:43 +00:00
2022-02-25 14:45:11 +00:00
// System configuration: type=Dedicated mode=Capped smt=4 lcpu=12 mem=24576MB
2021-06-08 18:24:43 +00:00
// System configuration: type=Dedicated mode=Donating smt=8 lcpu=16 mem=4096MB
2021-09-14 10:46:21 +00:00
private static final Pattern patternAixDedicated = Pattern . compile ( " ^System configuration: type=( \\ S+) mode=( \\ S+) smt=( \\ d+) lcpu=( \\ d+) mem=( \\ d+)MB " ) ;
2021-05-11 13:37:23 +00:00
2022-02-25 14:45:11 +00:00
2021-05-11 13:37:23 +00:00
// type=Shared mode=Uncapped smt=8 lcpu=4 mem=4101120 kB cpus=24 ent=4.00
2021-09-14 10:46:21 +00:00
private static final Pattern patternLinux = Pattern . compile ( " ^type=( \\ S+) mode=( \\ S+) smt=( \\ d+) lcpu=( \\ d+) mem=( \\ d+) kB cpus=( \\ d+) ent=( \\ d+ \\ .? \\ d*) " ) ;
2021-05-11 13:37:23 +00:00
2021-05-10 14:56:56 +00:00
2022-02-25 14:45:11 +00:00
2021-10-14 15:34:11 +00:00
private String type ; // Indicates the partition type. The value can be either dedicated or shared.
private String mode ; // Indicates whether the partition processor capacity is capped uncapped.
private int smt ; // Indicates whether simultaneous multithreading is enabled or disabled in the partition.
private int lcpu ; // Indicates the number of online logical processors.
//private int psize; // Indicates the number of online physical processors in the pool.
private float ent ; // Indicates the entitled processing capacity in processor units (shared mode only).
2021-05-10 14:56:56 +00:00
2021-05-21 15:00:47 +00:00
private final float user ; // Indicates the percentage of the entitled processing capacity used while executing at the user level (application).
private final float sys ; // Indicates the percentage of the entitled processing capacity used while executing at the system level (kernel).
private final float wait ; // Indicates the percentage of the entitled processing capacity unused while the partition was idle and had outstanding disk I/O request(s).
private final float idle ; // Indicates the percentage of the entitled processing capacity unused while the partition was idle and did not have any outstanding disk I/O request.
private final float physc ; // Indicates the number of physical processors consumed.
private final float entc ; // Indicates the percentage of the entitled capacity consumed.
private final float lbusy ; // Indicates the percentage of logical processor(s) utilization that occurred while executing at the user and system level.
2021-05-10 14:56:56 +00:00
2021-05-04 10:08:24 +00:00
2022-12-21 16:12:15 +00:00
public PowerProcessorStat ( InputStream inputStream ) throws IOException {
2021-05-10 14:56:56 +00:00
2021-06-10 06:55:03 +00:00
String lastLine = null ;
BufferedReader reader = new BufferedReader ( new InputStreamReader ( inputStream ) ) ;
while ( reader . ready ( ) ) {
String line = reader . readLine ( ) ;
2022-03-01 19:07:43 +00:00
log . trace ( " AixProcessorStat() - {} " , line ) ;
2021-05-10 14:56:56 +00:00
2021-05-11 13:37:23 +00:00
if ( line . startsWith ( " System configuration: " ) ) {
2021-06-08 18:24:43 +00:00
Matcher matcher = patternAixShared . matcher ( line ) ;
2021-05-11 13:37:23 +00:00
if ( matcher . find ( ) & & matcher . groupCount ( ) = = 7 ) {
type = matcher . group ( 1 ) ;
mode = matcher . group ( 2 ) ;
smt = Integer . parseInt ( matcher . group ( 3 ) ) ;
lcpu = Integer . parseInt ( matcher . group ( 4 ) ) ;
2021-10-14 15:34:11 +00:00
//psize = Integer.parseInt(matcher.group(6));
2021-05-11 13:37:23 +00:00
ent = Float . parseFloat ( matcher . group ( 7 ) ) ;
}
2021-06-08 18:24:43 +00:00
matcher = patternAixDedicated . matcher ( line ) ;
if ( matcher . find ( ) & & matcher . groupCount ( ) = = 5 ) {
type = matcher . group ( 1 ) ;
mode = matcher . group ( 2 ) ;
smt = Integer . parseInt ( matcher . group ( 3 ) ) ;
lcpu = Integer . parseInt ( matcher . group ( 4 ) ) ;
}
2021-05-11 13:37:23 +00:00
}
2021-05-10 14:56:56 +00:00
2021-05-11 13:37:23 +00:00
if ( line . startsWith ( " type= " ) ) {
//type=Shared mode=Uncapped smt=8 lcpu=4 mem=4101120 kB cpus=24 ent=4.00
Matcher matcher = patternLinux . matcher ( line ) ;
if ( matcher . find ( ) & & matcher . groupCount ( ) = = 7 ) {
type = matcher . group ( 1 ) ;
mode = matcher . group ( 2 ) ;
smt = Integer . parseInt ( matcher . group ( 3 ) ) ;
2021-10-14 15:34:11 +00:00
//psize = Integer.parseInt(matcher.group(4));
2021-05-11 13:37:23 +00:00
lcpu = Integer . parseInt ( matcher . group ( 4 ) ) ;
ent = Float . parseFloat ( matcher . group ( 7 ) ) ;
}
2021-05-10 14:56:56 +00:00
}
2021-05-11 13:37:23 +00:00
2021-06-10 06:55:03 +00:00
lastLine = line ;
2021-05-04 10:08:24 +00:00
}
2021-06-10 06:55:03 +00:00
//String lparstat = lines.get(lines.size() -1);
2021-09-10 19:58:14 +00:00
String [ ] splitStr = Objects . requireNonNull ( lastLine ) . trim ( ) . split ( " \\ s+ " ) ;
2022-02-25 14:45:11 +00:00
if ( type = = null | |
( mode . equalsIgnoreCase ( " Capped " ) & & splitStr . length < 4 ) | |
( type . equalsIgnoreCase ( " Shared " ) & & splitStr . length < 9 ) | |
( type . equalsIgnoreCase ( " Dedicated " ) & & mode . equalsIgnoreCase ( " Donating " ) & & splitStr . length < 8 )
) {
log . error ( " lparstat parse error - mode: {}, type: {}, content: {} " , mode , type , Arrays . toString ( splitStr ) ) ;
throw new UnsupportedOperationException ( " lparstat parse error. " ) ;
2021-05-10 14:56:56 +00:00
}
2021-05-04 10:08:24 +00:00
2021-05-10 14:56:56 +00:00
this . user = Float . parseFloat ( splitStr [ 0 ] ) ;
this . sys = Float . parseFloat ( splitStr [ 1 ] ) ;
this . wait = Float . parseFloat ( splitStr [ 2 ] ) ;
this . idle = Float . parseFloat ( splitStr [ 3 ] ) ;
2022-02-25 14:45:11 +00:00
if ( mode . equalsIgnoreCase ( " Uncapped " ) | | mode . equalsIgnoreCase ( " Donating " ) ) {
this . physc = Float . parseFloat ( splitStr [ 4 ] ) ;
} else {
this . physc = 0f ;
}
if ( type . equalsIgnoreCase ( " Shared " ) ) {
2021-06-08 18:24:43 +00:00
this . entc = Float . parseFloat ( splitStr [ 5 ] ) ;
this . lbusy = Float . parseFloat ( splitStr [ 6 ] ) ;
} else {
this . entc = 0f ;
this . lbusy = 0f ;
}
2021-06-10 06:55:03 +00:00
inputStream . close ( ) ;
2021-05-04 10:08:24 +00:00
}
2021-05-21 15:00:47 +00:00
public float getUser ( ) {
2021-05-10 14:56:56 +00:00
return user ;
2021-05-04 10:08:24 +00:00
}
2021-05-21 15:00:47 +00:00
public float getSys ( ) {
2021-05-10 14:56:56 +00:00
return sys ;
2021-05-04 10:08:24 +00:00
}
2021-05-21 15:00:47 +00:00
public float getIdle ( ) {
2021-05-10 14:56:56 +00:00
return idle ;
2021-05-04 10:08:24 +00:00
}
2021-05-21 15:00:47 +00:00
public float getWait ( ) {
2021-05-10 14:56:56 +00:00
return wait ;
2021-05-04 10:08:24 +00:00
}
2021-05-21 15:00:47 +00:00
public float getPhysc ( ) {
2021-05-10 14:56:56 +00:00
return physc ;
2021-05-04 10:08:24 +00:00
}
2021-05-21 15:00:47 +00:00
public float getEntc ( ) {
2021-05-10 14:56:56 +00:00
return entc ;
}
2021-05-04 10:08:24 +00:00
2021-05-21 15:00:47 +00:00
public float getLbusy ( ) {
2021-05-10 14:56:56 +00:00
return lbusy ;
2021-05-04 10:08:24 +00:00
}
2021-05-10 14:56:56 +00:00
public float getUsage ( ) {
return 100 - idle ;
2021-05-04 10:08:24 +00:00
}
2022-11-07 12:33:35 +00:00
public TreeMap < String , String > getTags ( ) {
return new TreeMap < > ( ) ;
2021-05-04 10:08:24 +00:00
}
2022-11-07 12:33:35 +00:00
public TreeMap < String , Object > getFields ( ) {
return new TreeMap < String , Object > ( ) { {
2021-09-14 10:46:21 +00:00
put ( " lcpu " , lcpu ) ;
put ( " ent " , ent ) ;
put ( " user " , user ) ;
put ( " sys " , sys ) ;
put ( " idle " , idle ) ;
put ( " wait " , wait ) ;
put ( " physc " , physc ) ;
put ( " entc " , entc ) ;
put ( " lbusy " , lbusy ) ;
put ( " mode " , mode ) ;
put ( " type " , type ) ;
2021-10-14 15:34:11 +00:00
put ( " smt " , smt ) ;
2021-09-14 10:46:21 +00:00
} } ;
2021-05-10 14:56:56 +00:00
}
2021-05-04 10:08:24 +00:00
}