<chapter id="rmacct.task.sgm"><title>Administering Extended Accounting (Tasks)</title><highlights><para>This chapter describes how to administer the extended accounting subsystem.</para><para>For an overview of the extending accounting subsystem, see <olink targetptr="rmacct-1" remap="internal">Chapter&nbsp;4, Extended Accounting (Overview)</olink>.</para>
</highlights><sect1 id="rmacct.task-25"><title>Administering the Extended Accounting Facility
(Task Map)</title><informaltable frame="all"><tgroup cols="3" colsep="1" rowsep="1"><colspec colwidth="33*"/><colspec colwidth="33*"/><colspec colwidth="33*"/><thead><row><entry><para>Task</para>
</entry><entry><para>Description</para>
</entry><entry><para>For Instructions</para>
</entry>
</row>
</thead><tbody><row><entry><para>Activate the extended accounting facility.</para>
</entry><entry><para>Use extended accounting to monitor resource consumption by each project
running on your system. You can use the <emphasis>extended accounting</emphasis> subsystem
to capture historical data for tasks, processes, and flows.</para>
</entry><entry><para><olink targetptr="rmacct.task-2" remap="internal">How to Activate Extended Accounting
for Processes, Tasks, and Flows</olink>, <olink targetptr="rmacct.task-8" remap="internal">How
to Activate Extended Accounting With a Startup Script</olink></para>
</entry>
</row><row><entry><para>Display extended accounting status.</para>
</entry><entry><para>Determine the status of the extended accounting facility.</para>
</entry><entry><para><olink targetptr="rmacct.task-9" remap="internal">How to Display Extended Accounting
Status</olink></para>
</entry>
</row><row><entry><para>View available accounting resources.</para>
</entry><entry><para>View the accounting resources available on your system.</para>
</entry><entry><para><olink targetptr="rmacct.task-10" remap="internal">How to View Available Accounting Resources</olink></para>
</entry>
</row><row><entry><para>Deactivate the process, task, and flow accounting facility.</para>
</entry><entry><para>Turn off the extended accounting functionality.</para>
</entry><entry><para><olink targetptr="rmacct.task-11" remap="internal">How to Deactivate Process, Task, and
Flow Accounting</olink></para>
</entry>
</row><row><entry><para>Use the Perl interface to the extended accounting facility.</para>
</entry><entry><para>Use the Perl interface to develop customized reporting and extraction
scripts.</para>
</entry><entry><para><olink targetptr="rmacct.task-19" remap="internal">Using the Perl Interface to libexacct</olink></para>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</sect1><sect1 id="rmacct.task-3"><title>Using Extended Accounting Functionality</title><task id="rmacct.task-2"><title>How to Activate Extended Accounting for Processes,
Tasks, and Flows</title><tasksummary><para>To activate the extended accounting facility for tasks, processes, and
flows, use the <command>acctadm</command> command. The optional final parameter
to <command>acctadm</command> indicates whether the command should act on
the process, system task, or flow accounting components of the extended accounting
facility.</para>
</tasksummary><procedure><step id="rmacct.task-step-4"><para>Become superuser or assume an equivalent
role.</para><para>Roles contain authorizations and privileged commands. For
more information about roles, see <olink targetdoc="sysadv1" targetptr="smcover-95" remap="external"><citetitle remap="section">Using the Solaris Management Tools With RBAC (Task Map)</citetitle> in <citetitle remap="book">System Administration Guide: Basic Administration</citetitle></olink>.</para>
</step><step id="rmacct.task-step-5"><para>Activate extended accounting for processes.</para><screen># <userinput>acctadm -e extended -f /var/adm/exacct/proc process</userinput></screen>
</step><step id="rmacct.task-step-6"><para>Activate extended accounting for tasks.</para><screen># <userinput>acctadm -e extended,mstate -f /var/adm/exacct/task task</userinput></screen>
</step><step id="rmacct.task-step-7"><para>Activate extended accounting for flows.</para><screen># <userinput>acctadm -e extended -f /var/adm/exacct/flow flow</userinput></screen>
</step>
</procedure><taskrelated role="see-also"><para>See <olink targetdoc="group-refman" targetptr="acctadm-1m" remap="external"><citerefentry><refentrytitle>acctadm</refentrytitle><manvolnum>1M</manvolnum></citerefentry></olink> for
more information.</para>
</taskrelated>
</task><task id="rmacct.task-8"><title>How to Activate Extended Accounting With a
Startup Script</title><procedure><step><para>Become superuser, or assume the Primary Administrator role.</para><para>To create the role and assign the role to a user, see <olink targetdoc="sysadv1" targetptr="smcover-95" remap="external"><citetitle remap="section">Using the Solaris Management Tools With RBAC (Task Map)</citetitle> in <citetitle remap="book">System Administration Guide: Basic Administration</citetitle></olink>.</para>
</step><step><para>Activate extended accounting on an ongoing basis by linking the <filename>/etc/init.d/acctadm</filename> script into <filename>/etc/rc2.d</filename>.</para><screen># <userinput>ln -s /etc/init.d/acctadm /etc/rc2.d/S<replaceable>n</replaceable>acctadm</userinput>
# <userinput>ln -s /etc/init.d/acctadm /etc/rc2.d/K<replaceable>n</replaceable>acctadm</userinput></screen><para>The <replaceable>n</replaceable> variable is replaced by a number.</para><tip><para>You must manually activate extended accounting at least once to
set up the configuration.</para>
</tip>
</step>
</procedure><taskrelated role="see-also"><para>See <olink targetptr="rmacct-5" remap="internal">Extended Accounting Configuration</olink> for
information on accounting configuration.</para>
</taskrelated>
</task><sect2 id="rmacct.task-9"><title>How to Display Extended Accounting Status</title><para>Type <command>acctadm</command> without arguments to display the current
status of the extended accounting facility.</para><screen>machine% <userinput>acctadm</userinput>
                 Task accounting: active
            Task accounting file: /var/adm/exacct/task
          Tracked task resources: extended
        Untracked task resources: none
              Process accounting: active
         Process accounting file: /var/adm/exacct/proc
       Tracked process resources: extended
     Untracked process resources: host
                 Flow accounting: active
            Flow accounting file: /var/adm/exacct/flow
          Tracked flow resources: extended
        Untracked flow resources: none</screen><para>In the previous example, system task accounting is active in extended
mode and <literal>mstate</literal> mode. Process and flow accounting are active
in extended mode.</para><note><para>In the context of extended accounting, microstate (<literal>mstate</literal>)
refers to the extended data, associated with microstate process transitions,
that is available in the process usage file (see <olink targetdoc="group-refman" targetptr="proc-4" remap="external"><citerefentry><refentrytitle>proc</refentrytitle><manvolnum>4</manvolnum></citerefentry></olink>). This data provides substantially more detail about
the activities of the process than basic or extended records.</para>
</note>
</sect2><sect2 id="rmacct.task-10"><title>How to View Available Accounting Resources</title><para>Available resources can vary from system to system, and from platform
to platform. Use the <command>acctadm</command> command with the <option>r</option> option
to view the accounting resources available on your system.</para><screen width="100">machine% <userinput>acctadm -r</userinput>
process:
extended pid,uid,gid,cpu,time,command,tty,projid,taskid,ancpid,wait-status,zone,flag,
memory,mstate<lineannotation>displays as one line</lineannotation>
basic    pid,uid,gid,cpu,time,command,tty,flag
task:
extended taskid,projid,cpu,time,host,mstate,anctaskid,zone
basic    taskid,projid,cpu,time
flow:
extended 
saddr,daddr,sport,dport,proto,dsfield,nbytes,npkts,action,ctime,lseen,projid,uid
basic    saddr,daddr,sport,dport,proto,nbytes,npkts,action</screen>
</sect2><task id="rmacct.task-11"><title>How to Deactivate Process, Task, and Flow
Accounting</title><tasksummary><para>To deactivate process, task, and flow accounting, turn off each of them
individually by using the <command>acctadm</command> command with the <option>x</option> option.</para>
</tasksummary><procedure><step id="rmacct.task-step-13"><para>Become superuser or assume an equivalent
role.</para><para>Roles contain authorizations and privileged commands. For
more information about roles, see <olink targetdoc="sysadv1" targetptr="smcover-95" remap="external"><citetitle remap="section">Using the Solaris Management Tools With RBAC (Task Map)</citetitle> in <citetitle remap="book">System Administration Guide: Basic Administration</citetitle></olink>.</para>
</step><step id="rmacct.task-step-14"><para>Turn off process accounting.</para><screen># <userinput>acctadm -x process</userinput> </screen>
</step><step id="rmacct.task-step-15"><para>Turn off task accounting.</para><screen># <userinput>acctadm -x task</userinput></screen>
</step><step id="rmacct.task-step-16"><para>Turn off flow accounting.</para><screen># <userinput>acctadm -x flow</userinput></screen>
</step><step id="rmacct.task-step-17"><para>Verify that task accounting, process
accounting, and flow accounting have been turned off.</para><screen>	# <userinput>acctadm</userinput>
            Task accounting: inactive
       Task accounting file: none
     Tracked task resources: extended
   Untracked task resources: none
         Process accounting: inactive
    Process accounting file: none
  Tracked process resources: extended
Untracked process resources: host
            Flow accounting: inactive
       Flow accounting file: none
     Tracked flow resources: extended
   Untracked flow resources: none</screen>
</step>
</procedure>
</task>
</sect1><sect1 id="rmacct.task-19"><title>Using the Perl Interface to <command>libexacct</command></title><sect2 id="rmacct.task-20"><title>How to Recursively Print the Contents of
an <literal>exacct</literal> Object</title><para>Use the following code to recursively print the contents of an <literal>exacct</literal> object. Note that this capability is provided by the library as
the <literal>Sun::Solaris::Exacct::Object::dump()</literal> function. This
capability is also available through the <literal>ea_dump_object()</literal> convenience
function.</para><screen>sub dump_object
     {
             my ($obj, $indent) = @_;
             my $istr = '  ' x $indent;

             #
             # Retrieve the catalog tag.  Because we are 
             # doing this in an array context, the
             # catalog tag will be returned as a (type, catalog, id) 
             # triplet, where each member of the triplet will behave as 
             # an integer or a string, depending on context.
             # If instead this next line provided a scalar context, e.g.
             #    my $cat  = $obj->catalog()->value();
             # then $cat would be set to the integer value of the 
             # catalog tag.
             #
             my @cat = $obj->catalog()->value();

             #
             # If the object is a plain item
             #
             if ($obj->type() == &amp;EO_ITEM) {
                     #
                     # Note: The '%s' formats provide s string context, so
                     # the components of the catalog tag will be displayed
                     # as the symbolic values. If we changed the '%s'
                     # formats to '%d', the numeric value of the components
                     # would be displayed.
                     #
                     printf("%sITEM\n%s  Catalog = %s|%s|%s\n", 
                        $istr, $istr, @cat);
                     $indent++;

                     #
                     # Retrieve the value of the item.  If the item contains
                     # in turn a nested exacct object (i.e., an item or
                     # group),then the value method will return a reference
                     # to the appropriate sort of perl object
                     # (Exacct::Object::Item or Exacct::Object::Group).
                     # We could of course figure out that the item contained
                     # a nested item orgroup by examining the catalog tag in
                     # @cat and looking for a type of EXT_EXACCT_OBJECT or
                     # EXT_GROUP.
                     #
                     my $val = $obj->value();
                     if (ref($val)) {
                             # If it is a nested object, recurse to dump it.
                             dump_object($val, $indent);
                     } else {
                             # Otherwise it is just a 'plain' value, so
                             # display it.
                             printf("%s  Value = %s\n", $istr, $val);
                     }

             #
             # Otherwise we know we are dealing with a group.  Groups
             # represent contents as a perl list or array (depending on
             # context), so we can process the contents of the group
             # with a 'foreach' loop, which provides a list context.
             # In a list context the value method returns the content
             # of the group as a perl list, which is the quickest
             # mechanism, but doesn't allow the group to be modified.
             # If we wanted to modify the contents of the group we could
             # do so like this:
             #    my $grp = $obj->value();   # Returns an array reference
             #    $grp->[0] = $newitem;
             # but accessing the group elements this way is much slower.
             #
             } else {
                     printf("%sGROUP\n%s  Catalog = %s|%s|%s\n",
                         $istr, $istr, @cat);
                     $indent++;
                     # 'foreach' provides a list context.
                     foreach my $val ($obj->value()) {
                             dump_object($val, $indent);
                     }
                     printf("%sENDGROUP\n", $istr);
             }
     }</screen>
</sect2><sect2 id="rmacct.task-21"><title>How to Create a New Group Record and Write
It to a File</title><para>Use this script to create a new group record and write it to a file
named <literal>/tmp/exacct</literal>.</para><screen width="80100">#!/usr/bin/perl

use strict;
use warnings;
use Sun::Solaris::Exacct qw(:EXACCT_ALL);
# Prototype list of catalog tags and values.
     my @items = (
             [ &amp;EXT_STRING | &amp;EXC_DEFAULT | &amp;EXD_CREATOR      => "me"       ],
             [ &amp;EXT_UINT32 | &amp;EXC_DEFAULT | &amp;EXD_PROC_PID     => $$         ],
             [ &amp;EXT_UINT32 | &amp;EXC_DEFAULT | &amp;EXD_PROC_UID     => $&lt;         ],
             [ &amp;EXT_UINT32 | &amp;EXC_DEFAULT | &amp;EXD_PROC_GID     => $(         ],
             [ &amp;EXT_STRING | &amp;EXC_DEFAULT | &amp;EXD_PROC_COMMAND => "/bin/rec" ],
     );

     # Create a new group catalog object.
     my $cat = ea_new_catalog(&amp;EXT_GROUP | &amp;EXC_DEFAULT | &amp;EXD_NONE)

     # Create a new Group object and retrieve its data array.
     my $group = ea_new_group($cat);
     my $ary = $group->value();

     # Push the new Items onto the Group array.
     foreach my $v (@items) {
             push(@$ary, ea_new_item(ea_new_catalog($v->[0]), $v->[1]));
     }

     # Open the exacct file, write the record &amp; close.
     my $f = ea_new_file('/tmp/exacct', &amp;O_RDWR | &amp;O_CREAT | &amp;O_TRUNC)
        || die("create /tmp/exacct failed: ", ea_error_str(), "\n");
     $f->write($group);
     $f = undef;</screen>
</sect2><sect2 id="rmacct.task-22"><title>How to Print the Contents of an <literal>exacct</literal> File</title><para>Use the following Perl script to print the contents of an <literal>exacct</literal> file.</para><screen>#!/usr/bin/perl

     use strict;
     use warnings;
     use Sun::Solaris::Exacct qw(:EXACCT_ALL);

     die("Usage is dumpexacct &lt;exacct file>\n") unless (@ARGV == 1);

     # Open the exact file and display the header information.
     my $ef = ea_new_file($ARGV[0], &amp;O_RDONLY) || die(error_str());
     printf("Creator:  %s\n", $ef->creator());
     printf("Hostname: %s\n\n", $ef->hostname());

     # Dump the file contents
     while (my $obj = $ef->get()) {
             ea_dump_object($obj);
     }

     # Report any errors
     if (ea_error() != EXR_OK &amp;&amp; ea_error() != EXR_EOF)  {
             printf("\nERROR: %s\n", ea_error_str());
             exit(1);
     }
     exit(0);</screen>
</sect2><sect2 id="rmacct.task-24"><title>Example Output From <literal>Sun::Solaris::Exacct::Object->dump()</literal></title><para>Here is example output produced by running <literal>Sun::Solaris::Exacct::Object->dump()</literal> on the file created in <olink targetptr="rmacct.task-21" remap="internal">How to
Create a New Group Record and Write It to a File</olink>.</para><screen>Creator:  root
Hostname: localhost
GROUP
       Catalog = EXT_GROUP|EXC_DEFAULT|EXD_NONE
       ITEM
         Catalog = EXT_STRING|EXC_DEFAULT|EXD_CREATOR
         Value = me
       ITEM
         Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_PID
         Value = 845523
       ITEM
         Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_UID
         Value = 37845
       ITEM
         Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_GID
         Value = 10
       ITEM
         Catalog = EXT_STRING|EXC_DEFAULT|EXD_PROC_COMMAND
         Value = /bin/rec
ENDGROUP</screen>
</sect2>
</sect1>
</chapter>