Using the Perl Interface to Extended Accounting
The Perl interface provides a Perl binding to the extended accounting tasks and projects. The interface allows the accounting files produced by the exacct framework to be read by Perl scripts. The interface also allows the writing of exacct files by Perl scripts.
This chapter includes the following topics:
Extended Accounting Overview
The exacct is a new accounting framework for the Solaris operating system provides additional functionality to that provided by the traditional SVR4 accounting mechanism. Traditional SVR4 accounting has these drawbacks:
The data collected by SVR4 accounting cannot be modified.
The type or quantity of statistics SVR4 accounting gathers cannot be customized for each application. Changes to the data SVR4 accounting collects would not work with all of the existing applications that use the accounting files.
The SVR4 accounting mechanism is not open.
Applications cannot embed their own data in the system accounting data stream.
The SVR4 accounting mechanism has no aggregation facilities.
The Solaris Operating system writes an individual record for each process that exists. No facilities are provided for grouping sets of accounting records into higher-level aggregates.
The exacct framework addresses the limitations of SVR4 accounting and provides a configurable, open, and extensible framework for the collection of accounting data.
The data that is collected can be configured using the exacct API.
Applications can either embed their own data inside the system accounting files, or create and manipulate their own custom accounting files.
The lack of data aggregation facilities in the traditional accounting mechanism are addressed by tasks and projects. Tasks identify a set of processes that are to be considered as a unit of work. Projects allow the processes executed by a set of users to be aggregated into a higher-level entity. See theproject(4) man page for more details about tasks and projects.
For a more extensive overview of extended accounting, see Chapter 4, "Extended Accounting (Overview)," in System Administration Guide: Solaris Containers-Resource Management and Solaris Zones.
Perl Interface to libexacct
Object Model
The Sun::Solaris::Exacct module is the parent of all the classes provided by libexacct(3LIB) library. libexacct(3LIB) provides operations on types of entities: exacct format files, catalog tags and exacct objects. exacct objects are subdivided into two types.
Items
Single data values
Groups
Lists of items
Benefits of Using the Perl Interface to libexacct
The Perl extensions to extended accounting provide a Perl interface to the underlying libexacct(3LIB) API and offer the following enhancements.
Full equivalence to C API provide a Perl interface that is functionally equivalent to the underlying C API.
The interface provides a mechanism for accessing exacct files that does not require C coding. All the functionality that is available from C is also available by using the Perl interface.
Ease of use.
Data obtained from the underlying C API is presented as Perl data types. Perl data types ease access to the data and remove the need for buffer pack and unpack operations.
Automated memory management.
The C API requires that the programmer take responsibility for managing memory when accessing exacct files. Memory management takes the form of passing the appropriate flags to functions, such as ea_unpack_object(3EXACCT), and explicitly allocating buffers to pass to the API. The Perl API removes these requirements, as all memory management is performed by the Perl library.
Prevent incorrect use of API.
The ea_object_t structure provides the in-memory representation of exacct records. The ea_object_t structure is a union type that is used for manipulating both Group and Item records. As a result, an incorrectly typed structure can be passed to some of the API functions. The addition of a class hierarchy prevents this type of programming error.
Perl Double-Typed Scalars
The modules described in this document make extensive use of the Perl double-typed scalar facility. The double-typed scalar facility allows a scalar value to behave either as an integer or as a string, depending upon the context. This behavior is the same as exhibited by the $! Perl variable (errno). The double-typed scalar facility avoids the need to map from an integer value into the corresponding string in order to display a value. The following example illustrates the use of double-typed scalars.
# Assume $obj is a Sun::Solaris::Item my $type = $obj->type(); # prints out "2 EO_ITEM" printf("%d %s\n", $type, $type); # Behaves as an integer, $i == 2 my $i = 0 + $type; # Behaves as a string, $s = "abc EO_ITEM xyx" my $s = "abc $type xyz"; |
Perl Modules
The various project, task and exacct-related functions have been separated into groups, and each group is placed in a separate Perl module. Each function has the Sun Microsystems standard Sun::Solaris:: Perl package prefix.
Table 4-1 Perl Modules
Module | Description |
---|---|
Provides functions to access the project manipulation functions: getprojid(2), setproject(3PROJECT), project_walk(3PROJECT), getprojent(3PROJECT), getprojbyname(3PROJECT), getprojbyid(3PROJECT), getdefaultproj(3PROJECT), inproj(3PROJECT), getprojidbyname(3PROJECT), setprojent(3PROJECT), endprojent(3PROJECT), fgetprojent(3PROJECT). | |
Provides functions to access the task manipulation functions settaskid(2) and gettaskid(2). | |
Top-level exacct module. Functions in this module access both the exacct-related system calls getacct(2), putacct(2), and wracct(2) as well as the libexacct(3LIB) library function ea_error(3EXACCT). This module contains constants for all the various exacct EO_*, EW_*, EXR_*, P_* and TASK_* macros. | |
Provides object-oriented methods to access the bitfields within an exacct catalog tag as well as the EXC_*, EXD_* and EXD_* macros. | |
Provides object-oriented methods to access the libexacct(3LIB) accounting file functions: ea_open(3EXACCT), ea_close(3EXACCT), ea_get_creator(3EXACCT), ea_get_hostname(3EXACCT), ea_next_object(3XACCT), ea_previous_object(3EXACCT), ea_write_object(3EXACCT). | |
Provides object-oriented methods to access the individual exacct accounting file object. An exacct object is represented as an opaque reference that is blessed into the appropriate Sun::Solaris::Exacct::Object subclass. This module is further subdivided into the two types of possible object: Item and Group. Methods are also provided to access the ea_match_object_catalog(3EXACCT), ea_attach_to_object(3EXACCT) functions. | |
Provides object-oriented methods to access an individual exacct accounting file Item. Objects of this type inherit from Sun::Solaris::Exacct::Object. | |
Provides object-oriented methods to access an individual exacct accounting file Group. Objects of this type inherit from Sun::Solaris::Exacct::Object, and provide access to the ea_attach_to_group(3EXACCT) function. The Items contained within the Group are presented as a perl array. | |
Private array type, used as the type of the array within a Sun::Solaris::Exacct::Object::Group. |