NAME

RPi::SysInfo - Retrieve hardware system information from a Raspberry Pi

DESCRIPTION

Fetch live-time and other system information from a Raspberry Pi.

Verified across the Raspberry Pi 3, 4 and 5 (the latter driven by the RP1 I/O controller), and across the Raspberry Pi OS releases from Buster through Bookworm/Trixie. Where tools or file locations changed between generations (raspi-gpio to pinctrl, /boot/config.txt to /boot/firmware/config.txt, the legacy vcgencmd get_camera to libcamera, ifconfig to ip), the relevant function selects whatever is present, preferring the modern tool and falling back to the legacy one.

Most functions will work equally as well on Unix/Linux systems.

SYNOPSIS

# Object Oriented

use RPi::SysInfo;

my $sys = RPi::SysInfo->new;
say $sys->cpu_percent;
say $sys->mem_percent;
say $sys->core_temp;

# Functional

use RPi::SysInfo qw(:all);

say cpu_percent();
say mem_percent();
say core_temp();

EXPORT_OK

Functions are not exported by default. You can load them each by name:

cpu_percent
mem_percent
core_temp
gpio_info
raspi_config
network_info
file_system
pi_details
pi_model

...or use the :all tag to bring them all in at once.

FUNCTIONS/METHODS

new

Instantiates and returns a new RPi::SysInfo object.

Takes no parameters.

cpu_percent

Returns the percentage of current CPU usage.

Takes no parameters.

Return: Two decimal floating point number.

mem_percent

Returns the percentage of physical RAM currently in use.

Takes no parameters.

Return: Two decimal floating point number.

core_temp($scale)

Returns the core CPU temperature of the system.

The value comes from vcgencmd measure_temp where available, falling back to the kernel thermal zone (/sys/class/thermal/thermal_zone0/temp) on systems without vcgencmd. Returns an empty string if neither source is readable.

Parameters:

$scale

Optional, String: By default we return the temperature in Celcius. Simply send in the letter f to get the result returned in Fahrenheit.

Return: Two decimal place floating point number.

gpio_info([$pins])

Fetches the current configuration and status of one or many GPIO pins.

Parameters:

$pins

Optional, Aref of Integers: By default, we'll return the information for all GPIO pins on the system. Send in an aref of pin numbers and well fetch the data for only those pins (eg: gpio_info[1] or gpio_info([2, 4, 6, 8])).

The data is collected with pinctrl (the current Raspberry Pi OS GPIO tool, and the only one available on the Pi 5 / RP1), falling back to the legacy raspi-gpio on older systems that still ship it. If neither tool is present, an empty string is returned.

Return: Single string containing all of the data requested.

raspi_config

Feteches the directive names and values the Pi is configured with. This includes the live vcgencmd get_config values plus the non-comment directives from the active config.txt (/boot/firmware/config.txt on Bookworm and later, falling back to /boot/config.txt).

Takes no parameters.

Return: String, the contents of the current configuration.

file_system

Fetches and returns various file system information as a string.

network_info

Fetches and returns the Pi's network configuration details as a string.

The data comes from ifconfig where the net-tools package is installed, falling back to ip addr (always present on current Raspberry Pi OS) when it is not. Returns an empty string if neither is available.

pi_details

Fetches and returns various information about the Pi, including the OS info, along with several hardware platform details as a string.

Includes the devicetree model, the os-release banner, uname, the tail of /proc/cpuinfo, a decoded board summary (see "pi_model" and the SoC/RAM/RP1 decode), the throttled flag, and camera status. Camera status uses the legacy vcgencmd get_camera on older firmware, falling back to a libcamera probe (rpicam-hello/libcamera-hello) on Bookworm and the Pi 5 where the legacy command was removed.

pi_model

Returns the normalized Raspberry Pi marketing name as a string, eg. Raspberry Pi 5 Model B Rev 1.1.

Takes no parameters.

The name is read from the devicetree model (authoritative on the Pi 0-5), falling back to a decode of the /proc/cpuinfo Revision code, and finally to Unknown if the board can't be identified.

Return: String.

PRIVATE FUNCTIONS/METHODS

_board_summary

Returns a human-readable SoC, RAM, RP1, manufacturer summary decoded from the /proc/cpuinfo Revision code, or unknown if it can't be decoded. Used to enrich "pi_details".

_camera_info

Returns a one-line camera status string. Tries the legacy vcgencmd get_camera first (older firmware), then a libcamera probe via "_camera_tool", then not detected.

_camera_tool

Returns the name of the libcamera listing utility found on PATH: rpicam-hello (Bookworm and later) or libcamera-hello (Bullseye), else undef.

_config_file

Returns the path to the active config.txt, preferring the Bookworm-and-later /boot/firmware/config.txt and falling back to the legacy /boot/config.txt. Returns undef if neither exists.

_core_temp_c

Returns the core temperature in Celsius as a number, or undef if unavailable. Prefers vcgencmd measure_temp, falling back to the kernel thermal zone.

_cpuinfo_field($field)

Returns the value of the named field from /proc/cpuinfo (eg. Revision, Serial, Model), or undef if the field or the file is absent.

Parameters:

$field

Mandatory, String: The /proc/cpuinfo field name to look up.

_decode_revision($revision)

Decodes a /proc/cpuinfo Revision code (hex string) into a hashref of board attributes (name, type, soc, mem, manufacturer, revision, rp1, new_style). Handles both the new-style (Pi 2+) and old-style (early Pi 1) encodings. Returns an empty hashref for an undefined or non-hex value.

Parameters:

$revision

Mandatory, String: The hex revision code.

_first_tool(@names)

Returns the first of the given executable names found on the PATH, or undef if none are present.

_format($float)

Formats a float/double value to two decimal places.

Parameters:

$float

Mandatory, Float/Double: The number to format.

_gpio_tool

Returns the name of the GPIO query tool found on PATH: pinctrl by preference, else the legacy raspi-gpio. Returns undef if neither is installed.

_mem_human($megabytes)

Formats a memory size in megabytes as a human string (eg. 512MB, 8GB). Returns undef for an undefined input.

Parameters:

$megabytes

Mandatory, Integer: The memory size in megabytes.

_net_tool

Returns the network-interface query command found on PATH: ifconfig by preference, else ip addr. Returns undef if neither is installed.

_run($cmd)

Runs a shell command and returns its output as a string (empty string if the command produced none or failed to execute). The single seam through which all external commands are executed, so tests can override it.

Parameters:

$cmd

Mandatory, String: The command to run.

_slurp($file)

Reads and returns the entire contents of a file, or undef if it can't be opened. The single seam through which all direct file reads happen, so tests can override it.

Parameters:

$file

Mandatory, String: The path to read.

AUTHOR

Steve Bertrand, <steveb at cpan.org>

LICENSE AND COPYRIGHT

Copyright 2019-2026 Steve Bertrand.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.