{"id":1027,"date":"2016-12-18T20:23:11","date_gmt":"2016-12-18T15:23:11","guid":{"rendered":"http:\/\/alexeyka.zantsev.com\/?p=1027"},"modified":"2017-07-21T18:11:15","modified_gmt":"2017-07-21T13:11:15","slug":"centos-7-2-1511-and-asterisk-13-1-max-open-files-problem","status":"publish","type":"post","link":"https:\/\/alexeyka.zantsev.com\/?p=1027","title":{"rendered":"CentOS 7.2.1511 and Asterisk 13.1: max open files problem"},"content":{"rendered":"<p>Asterisk 13.1 (running under CentOS 7.2.1511, maybe some other Asterisk versions too) has a problem: setting &#8216;maxfiles&#8217; in asterisk.conf does not matter.<br \/>\nSo, one day, your Asterisk may stop processing new calls under high load, and as the max opened files limit is reached, you will even have no opportunity to enter the Asterisk CLI.<\/p>\n<p>The \/var\/log\/asterisk\/messages (or Asterisk CLI, if you&#8217;ve been already logged in before reaching the max open files limit) will be filling with messages like these:<\/p>\n<pre>\r\n[Feb 13 16:47:00] WARNING[9283] acl.c: Cannot create socket\r\n[Feb 13 16:47:00] WARNING[9283] rtp.c: Unable to allocate RTCP socket: Too many open files\r\n[Feb 13 16:47:00] WARNING[14732] acl.c: Cannot create socket\r\n[Feb 13 16:47:00] WARNING[14732] channel.c: Channel allocation failed: Can't create alert pipe! \r\nTry increasing max file descriptors with ulimit -n\r\n<\/pre>\n<p>As a workaround, you may change the soft and hard limit for the Asterisk process on the OS level. But first of all let&#8217;s check how many files are allowed for Asterisk to be opened:<\/p>\n<pre>\r\nvoip-b2b01 ~ # cat \/proc\/`pidof asterisk`\/limits\r\nLimit                     Soft Limit           Hard Limit           Units     \r\nMax cpu time              unlimited            unlimited            seconds   \r\nMax file size             unlimited            unlimited            bytes     \r\nMax data size             unlimited            unlimited            bytes     \r\nMax stack size            8388608              unlimited            bytes     \r\nMax core file size        0                    unlimited            bytes     \r\nMax resident set          unlimited            unlimited            bytes     \r\nMax processes             15693                15693                processes \r\nMax open files            1024                 1024                 files     \r\nMax locked memory         65536                65536                bytes     \r\nMax address space         unlimited            unlimited            bytes     \r\nMax file locks            unlimited            unlimited            locks     \r\nMax pending signals       15693                15693                signals   \r\nMax msgqueue size         819200               819200               bytes     \r\nMax nice priority         0                    0                    \r\nMax realtime priority     0                    0                    \r\nMax realtime timeout      unlimited            unlimited            us        \r\n<\/pre>\n<p>So, you see the line with &#8216;1024&#8217; value, in spite of our setting in asterisk.conf:<\/p>\n<pre>\r\nMax open files            1024                 1024                 files     \r\n<\/pre>\n<p>Let&#8217;s fire! CentOS has a nice tool called prlimit. Use it:<\/p>\n<pre>\r\nprlimit --pid `pidof asterisk` --nofile=8192\r\n<\/pre>\n<p>Now let&#8217;s check again:<\/p>\n<pre>\r\nvoip-b2b01 ~ # cat \/proc\/`pidof asterisk`\/limits\r\nLimit                     Soft Limit           Hard Limit           Units     \r\n...\r\nMax open files            8192                 8192                 files     \r\n...\r\n<\/pre>\n<p>CentOS 7.2 uses Systemd, so it&#8217;s wise to edit Asterisk-related configuration file.<br \/>\nWe check the \/etc\/systemd\/system\/multi-user.target.wants\/ directory and see an &#8216;asterisk.service&#8217; file there, which is a symlink in fact, leading to \/usr\/lib\/systemd\/system\/asterisk.service:<\/p>\n<pre>\r\nvoip-b2b01 ~ # ls -l \/etc\/systemd\/system\/multi-user.target.wants\/\r\ntotal 0\r\nlrwxrwxrwx  1 root root 40 \u0438\u044e\u043b 14 13:09 asterisk.service -> \/usr\/lib\/systemd\/system\/asterisk.service\r\nlrwxrwxrwx. 1 root root 38 \u0441\u0435\u043d 15  2015 auditd.service -> \/usr\/lib\/systemd\/system\/auditd.service\r\nlrwxrwxrwx. 1 root root 41 \u0438\u044e\u043d 28 14:44 bacula-fd.service -> \/usr\/lib\/systemd\/system\/bacula-fd.service\r\nlrwxrwxrwx. 1 root root 39 \u0441\u0435\u043d 15  2015 chronyd.service -> \/usr\/lib\/systemd\/system\/chronyd.service\r\nlrwxrwxrwx. 1 root root 37 \u0441\u0435\u043d 15  2015 crond.service -> \/usr\/lib\/systemd\/system\/crond.service\r\nlrwxrwxrwx. 1 root root 42 \u0441\u0435\u043d 15  2015 irqbalance.service -> \/usr\/lib\/systemd\/system\/irqbalance.service\r\nlrwxrwxrwx. 1 root root 41 \u0438\u044e\u043d 28 12:33 nfs-client.target -> \/usr\/lib\/systemd\/system\/nfs-client.target\r\nlrwxrwxrwx. 1 root root 39 \u0438\u044e\u043d 28 12:49 oddjobd.service -> \/usr\/lib\/systemd\/system\/oddjobd.service\r\nlrwxrwxrwx. 1 root root 40 \u0441\u0435\u043d 15  2015 remote-fs.target -> \/usr\/lib\/systemd\/system\/remote-fs.target\r\nlrwxrwxrwx. 1 root root 39 \u0441\u0435\u043d 15  2015 rsyslog.service -> \/usr\/lib\/systemd\/system\/rsyslog.service\r\nlrwxrwxrwx  1 root root 40 \u0438\u044e\u043b 14 13:02 sendmail.service -> \/usr\/lib\/systemd\/system\/sendmail.service\r\nlrwxrwxrwx  1 root root 41 \u0438\u044e\u043b 14 13:02 sm-client.service -> \/usr\/lib\/systemd\/system\/sm-client.service\r\nlrwxrwxrwx. 1 root root 36 \u0441\u0435\u043d 15  2015 sshd.service -> \/usr\/lib\/systemd\/system\/sshd.service\r\nlrwxrwxrwx. 1 root root 36 \u0438\u044e\u043d 28 12:49 sssd.service -> \/usr\/lib\/systemd\/system\/sssd.service\r\nlrwxrwxrwx. 1 root root 44 \u0438\u044e\u043d 28 14:19 zabbix-agent.service -> \/usr\/lib\/systemd\/system\/zabbix-agent.service\r\n<\/pre>\n<p>Just uncomment\/edit the parameter LimitNOFILE. And I advise you to uncomment\/edit the Restart=always line to let Systemd control the Asterik is running (now you need not use Monit). You will have such a configuration:<\/p>\n<pre>\r\nvoip-b2b01 ~ # grep -vE '^$|^;|^#' \/usr\/lib\/systemd\/system\/asterisk.service \r\n[Unit]\r\nDescription=Asterisk PBX and telephony daemon.\r\nAfter=network.target\r\n\r\n[Service]\r\nType=simple\r\nEnvironment=HOME=\/var\/lib\/asterisk\r\nWorkingDirectory=\/var\/lib\/asterisk\r\nUser=asterisk\r\nGroup=asterisk\r\nExecStart=\/usr\/sbin\/asterisk -f -C \/etc\/asterisk\/asterisk.conf\r\nExecStop=\/usr\/sbin\/asterisk -rx 'core stop now'\r\nExecReload=\/usr\/sbin\/asterisk -rx 'core reload'\r\nLimitNOFILE=8192:8192\r\nRestart=always\r\n\r\nPrivateTmp=true\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n<\/pre>\n<p>Don&#8217;t forget to re-read settings by systemd:<\/p>\n<pre>\r\nsystemctl daemon-reload\r\n<\/pre>\n<p>&#8230; and now it&#8217;s time to restart Asterisk:<\/p>\n<pre>\r\nsystemctl restart asterisk.service\r\n<\/pre>\n<p>Well, at this point we&#8217;ve changed the max open files limit for the Asterisk process manually and changed the Systemd Asterisk config, hoping that it will start now with the 8192 max open files limit.<\/p>\n<p>If you&#8217;re interested in how many files are currently opened by your Asterisk, you may try:<\/p>\n<pre>\r\nvoip-b2b01 ~ # lsof -p `pidof asterisk` | wc -l\r\n956\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Asterisk 13.1 (running under CentOS 7.2.1511, maybe some other Asterisk versions too) has a problem: setting &#8216;maxfiles&#8217; in asterisk.conf does not matter. So, one day, your Asterisk may stop processing new calls under high load, and as the max opened files limit is reached, you will even have no opportunity to enter the Asterisk CLI. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[43],"class_list":["post-1027","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-asterisk"],"_links":{"self":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/1027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1027"}],"version-history":[{"count":8,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/1027\/revisions"}],"predecessor-version":[{"id":1079,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/1027\/revisions\/1079"}],"wp:attachment":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}