📜 ⬆️ ⬇️

Asterisk Managment Interface (AMI), Part 1

AMI is Asterisk's powerful and convenient software interface (API) for managing the system from external programs. In addition to AMI, AGI is often used - it is an interface for launching external applications that control the Asterisk channel within a particular call. Thanks to AMI, external programs can make connections with Asterisk via the TCP protocol, initiate command execution, read the result of their execution, as well as receive notifications about events in real time. These mechanisms can be used, for example, in the following cases:

AMI is often used to integrate with business processes and systems, CRM software (Customer Relationship Management). It can also be used for a variety of applications, such as automatic dialing programs and click-to-call systems (click-to-call).

Asterisk is often managed from the CLI console, but when using AMI, you do not need direct access to the server on which Asterisk is running. AMI is the simplest tool that, in the hands of a developer, can be a very powerful and flexible tool for integration with other software products. It allows developers to use the information generated by Asterisk in real time.

It is also worth noting that Asterisk since version 1.6 uses the interface manager version 1.1. Basically, the changes concerned the unification of many teams of the same type and the standardization of answers issued by various modules. You can find out the version of the interfaces using the CoreSettings command. The version may change further if the AMI interface loses full compatibility with previous versions.

How does AMI work


')
Between the Asterisk server and the client program, a simple, per-line protocol is used to establish a connection; each line of the message consists of two lines:

In the following, we will use the term “package”, which will describe a series of “key: value” constructions separated by CRLF and completed by the additional sequence CRLF.

The protocol of interaction between Asterisk and the client can be described by the following characteristics:

AMI accepts connections established on a network port (by default, TCP port 5038). The client program connects to the AMI through this port and is authenticated, then the Asterisk will respond to requests and also send notifications about changes in the state of the specified subsystems
Types of packages

The packet transmitted from the client to the Asterisk server and back is determined by the following key characteristics:

As a rule, the client sends Action packets to Asterisk (they are also called commands). The asterisk, in turn, executes the request and returns the result (often the result is the success of the action with a brief description in case of failure) received in the Response package. There is no guarantee regarding the order of arrival of results (response packages), therefore, in a client request, the ActionID parameter is usually included in each Action package, and the corresponding Response package will contain the same value in the ActionID field. Thus, the client can very easily process Action and Response packets, in any desired order, without waiting for Response packets to take the next action.

The following CLI command (Tab completion completes) helps to get a complete list of AMI commands available in your version of Asterisk:
*CLI> manager show commands

Packets Response. As written above, serve as responses to the transmitted commands. One response is sent to a command and may carry several values:

Event packages (events) are applied in two contexts. On the one hand, they inform the client about changes in the status of the Asterisk subsystems (eg. Channel status changes), on the other hand, they transfer the data set that Asterisk returns in response to Action.

Events are not documented in the CLI, so detailed information can be found in the documentation (file manager_1_1.txt), on voip-info.org. If you still have the impression that some event is not described, but it should be - you can find all sorts of events in the source code of the corresponding module by the function name - manager_event

Before turning to practical examples, it is worth noting that since version 1.6 of Asterisk, the AMI interface has acquired the version number 1.1. This is due to bringing the format of commands to a single format and partial loss of compatibility. The article will cover the work of version 1.6, where differences exist explanations will be given in brackets.
Security

Since the possibilities for managing the operation of the system provided through the AMI interface are almost endless. In addition to an unencrypted password and the ability to restrict access to AMI from certain ip-addresses, the Manager interface (as it is also called) does not have other security tools, so it is not recommended to use it on public IP addresses.

If the use of the public network is necessary, then it is necessary to ensure that access is limited using iptables or made through VPN tunnels of any type. Access to the AMI interface can also be obtained via the built-in HTTP (S) server, access is configured in the http.conf file. It is worth noting that, by default, the AMI interface is prohibited, just like working with it through an HTTP server.

Also on an unreliable network, if you want to provide end-user access to AMI (for example, click-2-call functionality via TAPI), AstManProxy is recommended to handle all connections to the Manager API. About him another time.

AMI Configuration



In order to use the AMI interface, you must edit the /etc/asterisk/manager.conf file, which is responsible for the configuration.

[general]
enabled=yes ; AMI (- no)
port=5038 ; TCP 5038
bindaddr=192.168.0.1 ; (0.0.0.0 - )
timestampevents = yes ;
displayconnects = yes ; AMI
allowmultiplelogin = yes ;

; ,
[admin] ; ,
secret=passwd1234 ; AMI
deny=0.0.0.0/0.0.0.0 ; ip-
permit=192.168.0.2/255.255.255.0 ; ip
read=system, call, log, verbose, command, agent, user ;
write=system, call,log, verbose, command, agent, user ;

The [general] section defines general connection settings. Enables AMI enabled = yes. Note: For the settings to take effect, you must do an AMI overload:

*CLI>module reload manager

or
*CLI>manager reload

Using the deny and permit options, you can specify ip-addresses from which it is possible to connect to Asterisk. You can add more system users (by copying the section starting with square brackets) and assign access rights to them:

In the above example, the admin user was given extensive rights, right up to stopping the Asterisk service (the command option is responsible for this). In subsequent versions (1.6.1 and later) it is forbidden to use commands in AMI, which can lead to the asterisk stopping.

Links




Source: https://habr.com/ru/post/64105/


All Articles