title: Cannot execute binary file - Zabbix external scripts on CentOS
body: <p>Problem: We use Zabbix as monitoring system. In addition to using its built in items, we also use something called external scripts feature (<a href="https://www.zabbix.com/documentation/3.0/manual/config/items/itemtypes/external" rel="nofollow noreferrer">this</a>), where custom scripts can be written and called via Zabbix. The problem facing here is its getting timed out. Script is simple expect file which goes inside a device and pulls some data. This works when called via root. But when called via Zabbix user, its complaining </p>
  
  <pre><code>/usr/bin/expect: /usr/bin/expect: cannot execute binary file
  </code></pre>
  
  <p>Script looks like this,</p>
  
  <pre><code>#!/usr/bin/expect
  set host "IP_ADDRESS"
  set uname "username"
  set pwd "password"
  set prompt "#|&gt;|:|\\\$";
  set val ""
  set domain [lindex $argv 0]
  log_user 0
  set timeout -1
  spawn /usr/bin/ssh "$uname@$host"
  expect "$uname@$host's password:"
  send "$pwd\n"
  sleep 1
  #expect -re "$prompt"
  expect "&gt;"
  sleep 1
  send "show wireless rf-domain statistics detail on $domain | grep Signals\r"
  sleep 1
  expect  "&gt;"
  set val $expect_out(buffer)
  send "exit\n"
  puts $val
  </code></pre>
  
  <p>This is named as rf_signal.exp. Its called via a wrapper shell script named rf_signal.</p>
  
  <pre><code>#!/bin/bash
  val=$(/usr/bin/expect '/usr/local/etc/scripts/rf_signal.exp' $1 | grep 'RF Signals' | cut -d':' -f2 | cut -d',' -f1 | cut -d' ' -f3 | sed -e 's/\s//g') 
  echo "$val"
  </code></pre>
  
  <p>And if  called as root, this works fine for example</p>
  
  <pre><code>[root@zbx-proxy2 externalscripts]# pwd
  /usr/local/share/zabbix/externalscripts
  [root@zbx-proxy2 externalscripts]# whoami
  root
  [root@zbx-proxy2 externalscripts]# /usr/local/share/zabbix/externalscripts/rf_signal DOMAIN_NAME
  241
  [root@zbx-proxy2 externalscripts]#
  </code></pre>
  
  <p>Where as if I call as zabbix user I am getting, cannot execute binary file error. With expect as a path</p>
  
  <pre><code>[root@zbx-proxy2 externalscripts]# runuser -l zabbix /usr/bin/expect /usr/local/share/zabbix/externalscripts/rf_signal 
  /usr/bin/expect: /usr/bin/expect: cannot execute binary file
  [root@zbx-proxy2 externalscripts]#
  </code></pre>
  
  <p>Without expect as a path, it waits - </p>
  
  <pre><code>[root@zbx-proxy2 externalscripts]# runuser -l zabbix  /usr/local/share/zabbix/externalscripts/rf_signal
  </code></pre>
  
  <p>PSTree command output shows it calls expect and contents inside the file </p>
  
  <pre><code>[root@zbx-proxy2 ~]# pstree -p 26295
  runuser(26295)---bash(26296)---bash(26309)-+-cut(26312)
                                             |-cut(26313)
                                             |-cut(26314)
                                             |-expect(26310)---ssh(26316)
                                             |-grep(26311)
                                             `-sed(26315)
  </code></pre>
  
  <p>PS details.</p>
  
  <pre><code>[root@zbx-proxy2 ~]# ps aux | grep zabbix  | grep -v "proxy\|agent\|fping"
  root     26295  0.0  0.0 130700  1388 pts/3    S+   15:46   0:00 runuser -l zabbix /usr/local/share/zabbix/externalscripts/rf_signal
  zabbix   26296  0.0  0.0 108184  1628 pts/3    S+   15:46   0:00 -bash /usr/local/share/zabbix/externalscripts/rf_signal
  zabbix   26309  0.0  0.0 108184   576 pts/3    S+   15:46   0:00 -bash /usr/local/share/zabbix/externalscripts/rf_signal
  zabbix   26310  0.0  0.0 115336  2260 pts/3    S+   15:46   0:00 /usr/bin/expect /usr/local/etc/scripts/rf_signal.exp
  zabbix   26311  0.0  0.0 103260   868 pts/3    S+   15:46   0:00 grep RF Signals
  zabbix   26312  0.0  0.0 100972   676 pts/3    S+   15:46   0:00 cut -d: -f2
  zabbix   26313  0.0  0.0 100972   672 pts/3    S+   15:46   0:00 cut -d, -f1
  zabbix   26314  0.0  0.0 100972   676 pts/3    S+   15:46   0:00 cut -d  -f3
  zabbix   26315  0.0  0.0 105268   872 pts/3    S+   15:46   0:00 sed -e s/\s//g
  zabbix   26316  0.0  0.0  59856  3220 pts/14   Ss+  15:46   0:00 /usr/bin/ssh username@IP_ADDRESS
  root     26688  0.0  0.0 105324   912 pts/7    S+   15:47   0:00 grep zabbix
  [root@zbx-proxy2 ~]#
  </code></pre>
  
  <p>All the scripts have read and execute permissions to all the users. And expect/grep/cut - whatever used inside the scripts are having read/execute permissions. What could be the issue?</p>