<!-- Acc Module User's Guide -->

<chapter>
    <chapterinfo>
	<revhistory>
	    <revision>
		<revnumber>$Revision: 1.7.2.1 $</revnumber>
		<date>$Date: 2005/06/22 23:12:00 $</date>
	    </revision>
	</revhistory>
    </chapterinfo>
    <title>User's Guide</title>
    
    <section>
	<title>Overview</title>
	<para>
	    acc module is used to report on transactions to syslog, <abbrev>SQL</abbrev> and
	    <acronym>RADIUS</acronym>.
	</para>
	<para>
	    To report on a transaction using syslog, use <quote>setflag</quote> to mark a
	    transaction you are interested in with a flag, load accounting module and set its
	    <quote>log_flag</quote> to the same flag number. The acc module will then report on
	    completed transaction to syslog. A typical usage of the module takes no acc-specific
	    script command -- the functionality binds invisibly through transaction processing.
	    Script writers just need to mark the transaction for accounting with proper setflag.
	</para>
	<para>
	    What is printed depends on module's <quote>log_fmt</quote> parameter. It's a string with
	    characters specifying which parts of request should be printed: 
	    <itemizedlist>
		<listitem>
		    <para>c = Call-Id</para>
		</listitem>
		<listitem>
		    <para>d = To tag (Dst)</para>
		</listitem>
		<listitem>
		    <para>f = From</para>
		</listitem>
		<listitem>
		    <para>i = Inbound Request-URI</para>
		</listitem>
		<listitem>
		    <para>m = Method</para>
		</listitem>
		<listitem>
		    <para>o = Outbound Request-URI</para>
		</listitem>
		<listitem>
		    <para>r = fRom</para>
		</listitem>
		<listitem>
		    <para>s = Status</para>
		</listitem>
		<listitem>
		    <para>t = To</para>
		</listitem>
		<listitem>
		    <para>u = digest Username</para>
		</listitem>
		<listitem>
		    <para>p = username Part of inbound Request-URI</para>
		</listitem>
	    </itemizedlist>
	    If a value is not present in request, "n/a" is accounted instead.
	</para>
	<para>
	    Note that:
	    <itemizedlist>
		<listitem>
		    <para>
			A single INVITE may produce multiple accounting reports -- that's
			due to SIP forking feature
		    </para>
		</listitem>
		<listitem>
		    <para>
			Subsequent ACKs and other requests do not hit the server and can't be
			accounted unless record-routing is enforced. The ACKs assert very little
			useful information anyway and reporting on INVITE's 200 makes most
			accounting scenarios happy.
		    </para>
		</listitem>
		<listitem>
		    <para>
			There is no session accounting -- ser maintains no sessions. If one needs to
			correlate INVITEs with BYEs for example for purpose of billing, then it is
			better done in the entity which collects accounting information. Otherwise,
			SIP server would have to become sessions-stateful, which would very badly
			impact its scalability.
		    </para>
		</listitem>
		<listitem>
		    <para>
			If a UA fails in middle of conversation, a proxy will never learn it.
			In general, a better practice is to account from an end-device
			(such as PSTN gateway), which best knows about call status
			(including media status and PSTN status in case of the gateway).
		    </para>
		</listitem>
	    </itemizedlist>
	</para>
	<para>
	    Support for SQL and RADIUS works analogously. You need to enable it by recompiling the
	    module with properly set defines. Uncomment the SQL_ACC and RAD_ACC lines in
	    modules/acc/Makefile. To compile SQL support, you need to have mysqlclient package on
	    your system. To compile RADIUS support, you need to have radiusclient installed on your
	    system (version 0.4.1 or higher is required) which is available from
	    <ulink url='http://developer.berlios.de/projects/radiusclient-ng/'>
	    http://developer.berlios.de/projects/radiusclient-ng/</ulink>. The radius client needs to
	    be configured properly. To do so, use the template at etc/radiusclient.conf and make sure
	    that module's radius_config parameter points to its location.  In particular, accounting
	    secret must match that one configured in server and proper dictionary is used (one is
	    available at etc/sip_dictionary). Uses along with FreeRadius (http://www.freeradius.org/)
	    and Radiator (http://www.open.com.au/radiator/) servers have been reported to us.
	</para>
	<para>
	    Both mysql and radius libraries must be dynamically linkable. You need to configure your
	    OS so that SER, when started, will find them. Typically, you do so by manipulating
	    LD_LIBRARY_PATH environment variable or configuring ld.so.
	</para>
	<section>
	    <title>General Example</title>
	    <programlisting format="linespecific">
loadmodule "modules/acc/acc.so"
modparam("acc", "log_level", 1)
modparam("acc", "log_flag", 1)

if (uri=~"sip:+49") /* calls to Germany */ {
    if (!proxy_authorize("iptel.org" /* realm */,
                         "subscriber" /* table name */))  {
        proxy_challenge("iptel.org" /* realm */, "0" /* no qop */ );
        break;
    }

    if (method=="INVITE" & !check_from()) {
        log("from!=digest\n");
        sl_send_reply("403","Forbidden");
        break;
    }

    setflag(1); /* set for accounting (the same value as in log_flag!)
    t_relay(); 	/* enter stateful mode now */
};
</programlisting>
	</section>

    </section>
    <section>
	<title>Dependencies</title>
	<para>
	    The module depends on the following modules (in the other words the listed modules
	    must be loaded before this module):
	    <itemizedlist>
		<listitem>
		    <para><emphasis>tm</emphasis> -- Transaction Manager</para>
		</listitem>
		<listitem>
		    <para><emphasis>a database module</emphasis> -- If compiled with database support.</para>
		</listitem>
	    </itemizedlist>
	</para>
    </section>
    <section>
	<title>Exported Parameters</title>
	<section>
	    <title><varname>secret</varname> (string)</title>
	    <para>
	    </para>
	    <para>
		Default value is randomly generated string.
	    </para>
	    <example>
		<title>Setting secret module parameter</title>
		<programlisting format="linespecific">
modparam("auth", "secret", "johndoessecretphrase")
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>log_level</varname> (integer)</title>
	    <para>
		Log level at which accounting messages are issued to syslog.
	    </para>
	    <para>
		Default value is L_NOTICE.
	    </para>
	    <example>
		<title>log_level example</title>
		<programlisting format="linespecific">
modparam("acc", "log_level", 2)   # Set log_level to 2
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>log_fmt</varname> (string)</title>
	    <para>
		Defines what parts of header fields will be printed to
		syslog, see <quote>overview</quote> for list of accepted values.
	    </para>
	    <para>
		Default value is <quote>miocfs</quote>.
	    </para>
	    <example>
		<title>log_fmt example</title>
		<programlisting format="linespecific">
modparam("acc", "log_fmt", "mfs")
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>early_media</varname> (integer)</title>
	    <para>
		Should be early media (183) accounted too ?
	    </para>
	    <para>
		Default value is 0 (no).
	    </para>
	    <example>
		<title>early_media example</title>
		<programlisting format="linespecific">
modparam("acc", "early_media", 1)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>failed_transactions</varname> (integer)</title>
	    <para>
		Should be failed transactions (status>=300) accounted too ?
	    </para>
	    <para>
		Default value is 0 (no).
	    </para>
	    <example>
		<title>failed_transactions example</title>
		<programlisting format="linespecific">
modparam("acc", "failed_transactions", 1)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>log_flag</varname> (integer)</title>
	    <para>
		Request flag which needs to be set to account a transaction.
	    </para>
	    <para>
		Default value is 1.
	    </para>
	    <example>
		<title>log_flag example</title>
		<programlisting format="linespecific">
modparam("acc", "log_flag", 2)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>log_missed_flag</varname> (integer)</title>
	    <para>
		Request flag which needs to be set to account missed calls.
	    </para>
	    <para>
		Default value is 2.
	    </para>
	    <example>
		<title>log_missed_flag example</title>
		<programlisting format="linespecific">
modparam("acc", "log_missed_flag", 3)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>report_ack</varname> (integer)</title>
	    <para>
		Shall acc attempt to account e2e ACKs too ? Note that this is really only an
		 attempt, as e2e ACKs may take a different path (unless RR enabled) and mismatch
		 original INVITE (e2e ACKs are a separate transaction).
	    </para>
	    <para>
		Default value is 1 (yes).
	    </para>
	    <example>
		<title>report_ack example</title>
		<programlisting format="linespecific">
modparam("acc", "report_ack", 0)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>report_cancels</varname> (integer)</title>
	    <para>
		By default, CANCEL reporting is disabled -- most accounting
		applications are happy to see INVITE's cancellation status.
		Turn on if you explicitly want to account CANCEL transactions.
	    </para>
	    <para>
		Default value is 0 (no).
	    </para>
	    <example>
		<title>report_cancels example</title>
		<programlisting format="linespecific">
modparam("acc", "report_cancels", 1)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>radius_config</varname> (string)</title>
	    <para>
		<emphasis>This parameter is radius specific.</emphasis> Path to radius client
		configuration file, set the referred config file correctly and specify there address
		of server, shared secret (should equal that in /usr/local/etc/raddb/clients for
		freeRadius servers) and dictionary, see etc for an example of config file and
		dictionary.
	    </para>
	    <para>
		Default value is <quote>/usr/local/etc/radiusclient/radiusclient.conf</quote>.
	    </para>
	    <example>
		<title>radius_config example</title>
		<programlisting format="linespecific">
modparam("acc", "radius_config", "/etc/radiusclient/radiusclient.conf")
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>service_type</varname> (integer)</title>
	    <para>
		Radius service type used for accounting.
	    </para>
	    <para>
		Default value is 15 (SIP).
	    </para>
	    <example>
		<title>service_type example</title>
		<programlisting format="linespecific">
modparam("acc", "service_type", 16)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>radius_flag</varname> (integer)</title>
	    <para>
		Request flag which needs to be set to account a transaction -- RADIUS specific.
	    </para>
	    <para>
		Default value is 1.
	    </para>
	    <example>
		<title>radius_flag example</title>
		<programlisting format="linespecific">
modparam("acc", "radius_flag", 2)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>radius_missed_flag</varname> (integer)</title>
	    <para>
		Request flag which needs to be set to account missed calls -- RADIUS specific.
	    </para>
	    <para>
		Default value is 2.
	    </para>
	    <example>
		<title>radius_missed_flag example</title>
		<programlisting format="linespecific">
modparam("acc", "radius_missed_flag", 3)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>db_url</varname> (string)</title>
	    <para>
		SQL address -- database specific.
	    </para>
	    <para>
		Default value is <quote>mysql://ser:heslo@localhost/ser</quote>
	    </para>
	    <example>
		<title>db_url example</title>
		<programlisting format="linespecific">
modparam("acc", "db_url", "mysql://user:password@localhost/ser")
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>db_flag</varname> (integer)</title>
	    <para>
		Request flag which needs to be set to account a transaction -- database specific.
	    </para>
	    <para>
		Default value is 1.
	    </para>
	    <example>
		<title>db_flag example</title>
		<programlisting format="linespecific">
modparam("acc", "db_flag", 2)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>db_missed_flag</varname> (integer)</title>
	    <para>
		Request flag which needs to be set to account missed calls -- database specific.
	    </para>
	    <para>
		Default value is 2.
	    </para>
	    <example>
		<title>db_missed_flag example</title>
		<programlisting format="linespecific">
modparam("acc", "db_missed_flag", 3)
</programlisting>
	    </example>
	</section>

	<section>
	    <title><varname>diameter_flag</varname> (integer)</title>
	    <para>
		Request flag which needs to be set to account a transaction -- DIAMETER specific.
	    </para>
	    <para>
		Default value is 1.
	    </para>
	    <example>
		<title>diameter_flag example</title>
		<programlisting format="linespecific">
modparam("acc", "diameter_flag", 2)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>diameter_missed_flag</varname> (integer)</title>
	    <para>
		Request flag which needs to be set to account missed calls -- DIAMETER specific.
	    </para>
	    <para>
		Default value is 2.
	    </para>
	    <example>
		<title>diameter_missed_flag example</title>
		<programlisting format="linespecific">
modparam("acc", "diameter_missed_flag", 3)
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>diameter_client_host</varname> (string)</title>
	    <para>
		Hostname of the machine where the DIAMETER Client is running -- DIAMETER specific.
	    </para>
	    <para>
		Default value is <quote>localhost</quote>.
	    </para>
	    <example>
		<title>diameter_client_host example</title>
		<programlisting format="linespecific">
modparam("acc", "diameter_client_host", "iptel.org")
</programlisting>
	    </example>
	</section>
	<section>
	    <title><varname>diameter_client_port</varname> (int)</title>
	    <para>
		Port number where the Diameter Client is listening -- DIAMETER specific.
	    </para>
	    <para>
		Default value is 3000.
	    </para>
	    <example>
		<title>diameter_client_host example</title>
		<programlisting format="linespecific">
modparam("acc", "diameter_client_port", 3000)
</programlisting>
	    </example>
	</section>

	<section>
	    <title><varname>db_table_acc</varname> (string)</title>
	    <para>
		Name of the table containing accounting information.
	    </para>
	    <para>
		Default value is "acc".
	    </para>
	</section>

	<section>
	    <title><varname>db_table_missed_calls</varname> (string)</title>
	    <para>
		Name of the table containing accounting information about
		missed calls.
	    </para>
	    <para>
		Default value is "missed_calls".
	    </para>
	</section>

	<section>
	    <title><varname>db_localtime</varname> (int)</title>
	    <para>
		The parameter controls how date and time information for
		accounted transactions will be stored in database. When set to
		1 all time-related information will be based on the local time
		zone. When set to 0 then acc module will use UTC.
	    </para>
	    <para>
		Default value is 0 (use UTC).
	    </para>
	</section>
	
	<section>
	    <title><varname>acc_sip_from_column</varname> (string)</title>
	    <para>
		Name of the column containing the contents of From header field.
	    </para>
	    <para>
		Default value is "sip_from".
	    </para>
	</section>

	<section>
	    <title><varname>acc_sip_to_column</varname> (string)</title>
	    <para>
		Name of the column containing the contents of To header field.
	    </para>
	    <para>
		Default value is "sip_to".
	    </para>
	</section>

	<section>
	    <title><varname>acc_sip_status_column</varname> (string)</title>
	    <para>
		Name of the column containing status code of SIP replies.
	    </para>
	    <para>
		Default value is "sip_status".
	    </para>
	</section>	
	
	<section>
	    <title><varname>acc_sip_method_column</varname> (string)</title>
	    <para>
		Name of the column containing the SIP method of the request.
	    </para>
	    <para>
		Default value is "sip_method".
	    </para>
	</section>

	<section>
	    <title><varname>acc_i_uri_column</varname> (string)</title>
	    <para>
		Name of the column containing the inbound Request-URI of the
		acccounted SIP message. This is the URI that SER received.
	    </para>
	    <para>
		Default value is "i_uri".
	    </para>
	</section>

	<section>
	    <title><varname>acc_o_uri_column</varname> (string)</title>
	    <para>
		Name of the column containing the outbound Request-URI of the
		accounted SIP message. This is the final URI, after applying
		all modifications from the configuration file. This is what the
		downstream element will receive in the Request-URI of the
		message forwarded by SER.
	    </para>
	    <para>
		Default value is "o_uri".
	    </para>
	</section>

	<section>
	    <title><varname>acc_sip_callid_column</varname> (string)</title>
	    <para>
		Name of the column containing the contents of Call-ID header field.
	    </para>
	    <para>
		Default value is "sip_callid".
	    </para>
	</section>

	<section>
	    <title><varname>acc_user_column</varname> (string)</title>
	    <para>
		Name of the column containing the username from digest credentials.
	    </para>
	    <para>
		Default value is "username".
	    </para>
	</section>

	<section>
	    <title><varname>acc_time_column</varname> (string)</title>
	    <para>
		Date and time of arrival of the final reply that finished the
		transaction.
	    </para>
	    <para>
		Default value is "time".
	    </para>
	</section>

	<section>
	    <title><varname>acc_from_uri_column</varname> (string)</title>
	    <para>
		Name of the column containing the SIP URI extracted from the
		From header field. This is usually the part enclosed in angle
		brackets.
	    </para>
	    <para>
		Default value is "from_uri".
	    </para>
	</section>		

	<section>
	    <title><varname>acc_to_uri_column</varname> (string)</title>
	    <para>
		Name of the column containing the SIP URI extracted from the
		To header field. This is usually the part enclosed in angle
		brackets.
	    </para>
	    <para>
		Default value is "to_uri".
	    </para>
	</section>

	<section>
	    <title><varname>acc_totag_column</varname> (string)</title>
	    <para>
		Name of the column containing the value of the To header field
		tag parameter.
	    </para>
	    <para>
		Default value is "totag".
	    </para>
	</section>

	<section>
	    <title><varname>acc_fromtag_column</varname> (string)</title>
	    <para>
		Name of the column containing the value of the From header
		field tag parameter.
	    </para>
	    <para>
		Default value is "fromtag".
	    </para>
	</section>

	<section>
	    <title><varname>acc_domain_column</varname> (string)</title>
	    <para>
		The name of the column containing the name of domain. This is
		used when SER is operated in multi-domain mode.
	    </para>
	    <para>
		Default value is "domain".
	    </para>
	</section>
	
    </section>
    <section>
	<title>Exported Functions</title>
	<section>
	    <title><function moreinfo="none">acc_log_request(comment)</function></title>
	    <para>
		<function moreinfo="none">acc_request</function> reports on a request, for example,
		it can be used to report on missed calls to off-line users who are replied 404. To
		avoid multiple reports on UDP request retransmission, you would need to embed the
		action in stateful processing.
	    </para> 
	    <para>
		Meaning of the parameters is as follows:</para>
	    <itemizedlist>
		<listitem>
		    <para><emphasis>comment</emphasis> - Comment to be appended.
		    </para>
		</listitem>
	    </itemizedlist>
	    <example>
		<title>acc_log_request usage</title>
		<programlisting format="linespecific">
...
acc_log_request("Some comment");
...
</programlisting>
	    </example>
	</section>
	<section>
	    <title><function moreinfo="none">acc_db_request(comment, table)</function></title>
	    <para>
		Like <function moreinfo="none">acc_log_request</function>, <function
		moreinfo="none">acc_db_request</function> reports on a request. The report is sent
		to database at <quote>db_url</quote>, in the table referred to in the second action
		parameter
		</para>
	    <para>
		Meaning of the parameters is as follows:
	    </para>
	    <itemizedlist>
		<listitem>
		    <para><emphasis>comment</emphasis> - Comment to be appended.</para>
		</listitem>
		<listitem>
		    <para><emphasis>table</emphasis> - Database table to be used.</para>
		</listitem>
	    </itemizedlist>
	    <example>
		<title>acc_db_request usage</title>
		<programlisting format="linespecific">
...
acc_log_request("Some comment", "Some table");
...
</programlisting>
	    </example>
	</section>
	<section>
	    <title><function moreinfo="none">acc_rad_request(comment)</function></title>
	    <para>
		Like <function moreinfo="none">acc_log_request</function>, 
		<function moreinfo="none">acc_rad_request</function> reports on a request. It
		reports to radius server as configured in <quote>radius_config</quote>.
	    </para> 
	    <para>
		Meaning of the parameters is as follows:</para>
	    <itemizedlist>
		<listitem>
		    <para><emphasis>comment</emphasis> - Comment to be appended.
		    </para>
		</listitem>
	    </itemizedlist>
	    <example>
		<title>acc_rad_request usage</title>
		<programlisting format="linespecific">
...
acc_rad_request("Some comment");
...
</programlisting>
	    </example>
	</section>
	<section>
	    <title><function moreinfo="none">acc_diam_request(comment)</function></title>
	    <para>
		Like <function moreinfo="none">acc_log_request</function>, 
		<function moreinfo="none">acc_diam_request</function> reports on a request. It
		reports to Diameter server.
	    </para> 
	    <para>
		Meaning of the parameters is as follows:</para>
	    <itemizedlist>
		<listitem>
		    <para><emphasis>comment</emphasis> - Comment to be appended.
		    </para>
		</listitem>
	    </itemizedlist>
	    <example>
		<title>acc_diam_request usage</title>
		<programlisting format="linespecific">
...
acc_diam_request("Some comment");
...
</programlisting>
	    </example>
	</section>
    </section>
</chapter>

<!-- Keep this element at the end of the file
Local Variables:
sgml-parent-document: ("acc.sgml" "Book" "chapter")
End:
-->
