Upgrade 3CX to v18 and get it hosted free!

Asterisk cmd ReadFile

Author image

Reads the contents of a file into a variable.

Description

ReadFile(varname=file,length)

varname: The result will be stored here
file: The name of the file to read
length: Maximum number of characters to capture

Example 1

exten => s,1,ReadFile(foo=/ect/asterisk/extensions.conf,20);

Example 2: CPU load (Asterisk 1.4)

Use Set to read single variable into the dialplan, or use a While loop or the Asterisk func array function to read multiple variables.
Here is an example using ARRAY() – note that in Asterisk 1.6.x we could use the Asterisk func shell function together with Asterisk func cut instead of the clumsy TrySystem() combined with Set() and Asterisk func eval:

exten => s,1, TrySystem(uptime \| tr -d “\,” \| cut -d “:” -f 5 \| awk \'{print \”ARRAY(load1\,load5\,load15)=\” $1\”\\\\\,\” $2\”\\\\\,\” $3}\’ > /tmp/cpu-load.txt)
exten => s,n,TrySystem(cat cpu-load.txt | wc -m > /tmp/cpu-load-length.txt)
exten => s,n,ReadFile(load_length=/tmp/cpu-load-length.txt,2)
exten => s,n,NoOp(load_length=${load_length})
exten => s,n,ReadFile(load_array=/tmp/cpu-load.txt,${MATH(${load_length}-1,int)}) ;remove trailing special character
exten => s,n,NoOp(load_array=${load_array})
exten => s,n,Set(${EVAL(${load_array})})
exten => s,n,NoOp(load1=${load1} load5=${load5} load15=${load15})

This will, after some slightly crazy backslash escaping in extensions.conf of Asterisk 1.4, execute

uptime | tr -d “,” | cut -d “:” -f 5 | awk ‘{print “ARRAY(load1,load5,load15)=” $1″\\,” $2″\\,” $3}’ > /tmp/cpu-load.txt

and as a result write the current CPU load averages for 1 minute, 5 minutes and 15 minutes (as shown by ‘top’) into a file like this:

ARRAY(load1,load5,load15)=0.50\,0.31\,0.24

Note: The above might fail if your system’s uptime is less than 1 hour or less than 2 days. Change ‘-f 5’ into ‘-f 4’ or ‘-f 3’.
Here is a more reliable way to extract the load values that you can put into a shell script, and then execute with TrySystem():

x=`uptime`; echo ${x#*average:} | tr -d “,” | awk ‘{print “ARRAY(load1,load5,load15)=” $1″\\,” $2″\\,” $3}’ > /tmp/cpu-load.txt

You could now create a macro for the above, call it with the M() option of the Dial command, and store the CPU load, together with the channel’s codec and some RTCP statistics, in the userfield of your CDR log (or export them to the intial inbound channel with the help of the SHARED function).

See also



Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.