Metric
Metric
Class Metric.
class MetricProvides functionality for handling file metrics within the framework. Typically used for measuring and representing file sizes or related metrics.
Properties
file
Store the file.
private string $fileversion
Store the version.
private int $version = 1tickSec
Store the tick sec.
private int $tickSechandle
File handle resource.
private mixed $handledataSources
A list of data sources defined in this metric file.
private array $dataSourcesEach data source is an associative array containing its name, description, type, tick count, min/max values, and last tick index. Example:
[
'speed' => [
'name' => 'speed',
'desc' => 'Speed Counter',
'type' => 0x02, // COUNTER
'ticks' => 600,
'min' => 0,
'max' => null,.
'last' => -1,
],
'temperature' => [
'name' => 'temperature',
'desc' => 'Temperature Gauge',
'type' => 0x01, // GAUGE
'ticks' => 60,
'min' => -40,
'max' => 100,
'last' => -1,
],
]The 'type' field indicates the data source type (e.g., GAUGE, COUNTER, ABSOLUTE). The 'ticks' field indicates how many ticks are stored for this data source before consolidation. The 'last' field tracks the index of the last tick written for this data source.
Note: The actual structure and fields of the data source may vary based on implementation details and requirements. The 'last' field is initialized to -1 to indicate that no ticks have been written yet. The 'min' and 'max' fields can be used to enforce value limits for the data source, where null indicates no limit.
This array is used internally to manage the data sources and their associated metadata when writing to the metric file.
The data sources are stored in the metric file with a specific format, and this array helps to keep track of their definitions and current state during runtime. The 'ticks' field is crucial for determining when to consolidate data points into the archive based on the defined tick count for each data source. The 'type' field is used to determine how values are updated for the data source (e.g., whether to sum values for a COUNTER or take the maximum for a GAUGE). The 'desc' field provides a human-readable description of the data source, which can be useful for documentation or display purposes when analyzing the metrics. Overall, this array serves as the central structure for managing the data sources within the Metric class and ensuring that they are correctly defined and updated according to the specified configuration.
archives
private array $archiveslastTick
private array $lastTick = array (
'data' =>
array (
),
'archive' =>
array (
),
)dataSourceTypes
private array $dataSourceTypes = array (
'GAUGE' => 0,
'COUNTER' => 0,
'ABSOLUTE' => 0,
'GAUGEZ' => 0,
)archiveCFs
private array $archiveCFs = array (
'AVERAGE' => 0,
'MIN' => 0,
'MAX' => 0,
'LAST' => 0,
'COUNT' => 0,
)Methods
__construct
Create a new instance.
public __construct(string $file): voidParameters
| Parameter | Type | Description |
|---|---|---|
$file | string |
__destruct
Clean up resources before destruction.
public __destruct(): voidexists
Determine whether a path exists in backend storage.
public exists(): booladdDataSource
addDataSource('speed', 'COUNTER', 0, 600, 'Speed Counter');.
public addDataSource(string $dsname, string $type, ?int $min, ?int $max, ?string $description): voidParameters
| Parameter | Type | Description |
|---|---|---|
$dsname | string | //The name of the data source |
$type | string | //Data source types are: GAUGE, COUNTER, ABSOLUTE |
$min | int | //Minimum allowed value |
$max | int | //Maximum allowed value |
$description | string | //String describing the data source |
addArchive
addArchive('day_average', 'AVERAGE', 60, 24);.
public addArchive(string $archiveID, string $cf, ?int $ticks, ?int $rows, ?string $description): voidParameters
| Parameter | Type | Description |
|---|---|---|
$archiveID | string | Name of the archive |
$cf | string | Consolidation function and can be: AVERAGE, MIN, MAX or LAST |
$ticks | int | Number of ticks to consolidate into a row |
$rows | int | Number of rows to store in the archive |
$description | string | A string describing the archive |
getTick
Get a tick value.
public getTick(?int $time): intParameters
| Parameter | Type | Description |
|---|---|---|
$time | int | Defaults to the current time if not specified |
create
Execute the create operation for this file component.
public create(int $tickSec = 1): boolParameters
| Parameter | Type | Description |
|---|---|---|
$tickSec | int |
setValue
Set a value for a data source.
public setValue(string $dsname, float $value): boolParameters
| Parameter | Type | Description |
|---|---|---|
$dsname | string | |
$value | float |
update
The update function stores a consolidated data point in the archive for each data source based on the settings
public update(): boolsupplied when defining the archive.
- A primary data point is a single value stored for each 'tick'.
- A consolidated data point is a value calculated based on 1 or more primary data points using the consolidation function specified when defining the archive.
How this works is as follows:
- Step 1: Load all the available data points ready to be processed
- Step 2: Make sure there are data points for all ticks from lastTick to currentTick
- Step 3: Check if there are enough primary data points to create a consolidated data point.
- Step 4: If so, apply the consolidation function
- Step 5: Update the starting point in the archive definition
- Step 6: Store the consolidated data point value in the archive.
hasDataSource
Check whether the instance s data source.
public hasDataSource(string $name): boolParameters
| Parameter | Type | Description |
|---|---|---|
$name | string |
listDataSources
Retrieve the names of all data sources defined in this metric file.
public listDataSources(): voidgetDataSource
Retrieves the data source by its name.
public getDataSource(string $name): voidParameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the data source to retrieve |
listArchives
Retrieve the names of all archives defined in this metric file.
public listArchives(): voidgetArchive
Retrieves the archive data associated with the given ID.
public getArchive(string $id): voidParameters
| Parameter | Type | Description |
|---|---|---|
$id | string | the identifier of the archive to retrieve |
getfile
Execute the getfile operation for this file component.
public getfile(): stringgraph
Generates a graph matrix for the specified data source and archive.
public graph(string $dsname, string $archiveID = 'default', false $): voidParameters
| Parameter | Type | Description |
|---|---|---|
$dsname | string | the name of the data source to generate the graph for |
$archiveID | string | the ID of the archive to use (default is 'default') |
$ | false |
data
Retrieve the raw primary data points stored in a data source.
public data(string $dsname, false $): voidParameters
| Parameter | Type | Description |
|---|---|---|
$dsname | string | the name of the data source |
$ | false |
getCDPLength
Calculate the length of a row within an archive.
private getCDPLength(): intgetDataSourceOffset
Calculate the start position in the file of a data source.
private getDataSourceOffset(NULL $dsname): intParameters
| Parameter | Type | Description |
|---|---|---|
$dsname | NULL | The name of the data source. If omitted, returns the position of the byte after all data sources |
getArchiveOffset
Calculate the start position in the file of an archive.
private getArchiveOffset(string $archiveID = 'default'): intParameters
| Parameter | Type | Description |
|---|---|---|
$archiveID | string | The name of the archive |
restoreOptions
Load and restore data sources and archives from an existing RRD database file.
private restoreOptions(): boolwriteDataSource
Writes a data source definition to the metric file.
private writeDataSource(array $ds): boolThis method calculates the required number of primary data points (ticks) for the data source based on the archives, packs the data source information into a binary string, and writes it to the file handle. It also writes the primary data points (PDPs) for the data source.
Parameters
| Parameter | Type | Description |
|---|---|---|
$ds | array |
writeArchive
Writes an archive header and initializes archive data to the file handle.
private writeArchive(array $archive): boolConstructs the archive header using packed binary data, including metric type, archive ID, description, consolidation function, ticks, rows, and last timestamp. Validates the header length against the expected constant METRIC_ARCHIVE_HDR_LEN. Initializes archive rows with zeroed data points for each data source.
Parameters
| Parameter | Type | Description |
|---|---|---|
$archive | array |
writeCDP
private writeCDP(int $tick, array $values, bool $doWrite = true): stringParameters
| Parameter | Type | Description |
|---|---|---|
$tick | int | |
$values | array | |
$doWrite | bool |
consolidate
Consolidates an array of data points based on the specified consolidation function (cf) and data source type (dsType).
private consolidate(int $cf, array $dataPoints, ?int $dsType): floatSupported consolidation functions:
- 0x01: AVERAGE - Calculates the average of the data points.
- 0x02: MIN - Finds the minimum value among the data points.
- 0x03: MAX - Finds the maximum value among the data points.
- 0x04: LAST - Returns the last data point.
- 0x05: SUM - Calculates the sum of the data points.
If dsType is 0x04 (GAUGEZ), zero values are ignored during consolidation.
Parameters
| Parameter | Type | Description |
|---|---|---|
$cf | int | consolidation function identifier |
$dataPoints | array | array of numeric data points to consolidate |
$dsType | int | optional data source type identifier |
Generated by Hazaar API Doc Generator on Tue, 21 Apr 2026 04:00:24 +0000