1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178:
<?php
namespace Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Parser;
use InvalidArgumentException;
use JonasRudolph\PHPComponents\StringUtility\Implementation\StringUtility;
use Net\Bazzline\Component\ApacheServerStatusParser\DomainModel\Statistic;
class StatisticListOfLineParser implements ListOfLineParserInterface
{
private $stringUtility;
public function __construct(
StringUtility $stringUtility
) {
$this->stringUtility = $stringUtility;
}
public function parse(array $listOfLine)
{
$stringUtility = $this->stringUtility;
$listOfLineHasMinimalSize = (count($listOfLine) > 9);
if ($listOfLineHasMinimalSize) {
$listOfMandatoryPropertyNameToStartsWithPrefix = [
'current_timestamp' => 'Current Time: ',
'cpu_usage' => 'CPU Usage: ',
'parent_server_configuration_generation' => 'Parent Server Config. Generation: ',
'parent_server_mpm_generation' => 'Parent Server MPM Generation: ',
'restart_time' => 'Restart Time: ',
'server_up_time' => 'Server uptime: ',
'server_load' => 'Server load: ',
'total_accesses' => 'Total accesses: '
];
$listOMandatoryProperties = [
'b_per_seconds' => null,
'current_time' => null,
'cpu_load' => null,
'cpu_usage' => null,
'idle_workers' => null,
'kb_per_seconds' => null,
'parent_server_configuration_generation' => null,
'parent_server_mpm_generation' => null,
'request_per_seconds' => null,
'request_currently_being_processed' => null,
'restart_time' => null,
'server_up_time' => null,
'server_load' => null,
'total_accesses' => null,
'total_traffic' => null
];
foreach ($listOfLine as $line) {
if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['current_timestamp'])) {
$listOMandatoryProperties['current_time'] = strtotime(
substr($line, 14)
);
} else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['cpu_usage'])) {
$lineAsArray = explode(' - ', $line);
$listOMandatoryProperties['cpu_load'] = filter_var(
$lineAsArray[1],
FILTER_SANITIZE_NUMBER_INT
);
$listOMandatoryProperties['cpu_usage'] = substr($lineAsArray[0], 11);
} else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['parent_server_configuration_generation'])) {
$listOMandatoryProperties['parent_server_configuration_generation'] = substr($line, 34);
} else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['parent_server_mpm_generation'])) {
$listOMandatoryProperties['parent_server_mpm_generation'] = substr($line, 30);
} else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['restart_time'])) {
$listOMandatoryProperties['restart_time'] = strtotime(
substr($line, 14)
);
} else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['server_up_time'])) {
$listOMandatoryProperties['server_up_time'] = substr($line, 16);
} else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['server_load'])) {
$listOMandatoryProperties['server_load'] = substr($line, 13);
} else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['total_accesses'])) {
$lineAsArray = explode(' - ', $line);
$listOMandatoryProperties['total_accesses'] = substr($lineAsArray[0], 16);
$listOMandatoryProperties['total_traffic'] = substr($lineAsArray[1], 15);
} else if ($stringUtility->endsWith($line, 'request')) {
$lineAsArray = explode(' - ', $line);
$listOMandatoryProperties['b_per_seconds'] = filter_var(
$lineAsArray[1],
FILTER_SANITIZE_NUMBER_INT
);
$listOMandatoryProperties['kb_per_seconds'] = filter_var(
$lineAsArray[2],
FILTER_SANITIZE_NUMBER_INT
);
$listOMandatoryProperties['request_per_seconds'] = filter_var(
$lineAsArray[0],
FILTER_SANITIZE_NUMBER_INT
);
} else if ($stringUtility->endsWith($line, 'workers')) {
$lineAsArray = explode(',', $line);
$listOMandatoryProperties['idle_workers'] = filter_var(
$lineAsArray[1],
FILTER_SANITIZE_NUMBER_INT
);
$listOMandatoryProperties['request_currently_being_processed'] = filter_var(
$lineAsArray[0],
FILTER_SANITIZE_NUMBER_INT
);
}
}
foreach ($listOMandatoryProperties as $name => $mandatoryProperty) {
if (is_null($mandatoryProperty)) {
throw new InvalidArgumentException(
'could not find mandatory property "'
. $listOfMandatoryPropertyNameToStartsWithPrefix[$name]
. '"'
);
}
}
$statistic = new Statistic(
$listOMandatoryProperties['b_per_seconds'],
$listOMandatoryProperties['cpu_load'],
$listOMandatoryProperties['cpu_usage'],
$listOMandatoryProperties['current_time'],
$listOMandatoryProperties['idle_workers'],
$listOMandatoryProperties['kb_per_seconds'],
$listOMandatoryProperties['parent_server_configuration_generation'],
$listOMandatoryProperties['parent_server_configuration_generation'],
$listOMandatoryProperties['request_currently_being_processed'],
$listOMandatoryProperties['request_per_seconds'],
$listOMandatoryProperties['restart_time'],
$listOMandatoryProperties['server_load'],
$listOMandatoryProperties['server_up_time'],
$listOMandatoryProperties['total_accesses'],
$listOMandatoryProperties['total_traffic']
);
} else {
throw new InvalidArgumentException(
self::class . ' can not parse given list of line "'
. implode(',', $listOfLine) . '"'
);
}
return $statistic;
}
}