Grep command giving different result for different users for same command

Hello,

I am running below command as root user

#nodetool cfstats tests | grep  "Memtable switch count"
Memtable switch count: 12

Where as when I try to run same command as another user it gives different result.

#su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch count""
Memtable cell count: 0
Memtable data size: 0
Memtable off heap memory used: 0
Memtable switch count: 12

I am not sure why grep giving different result here.

Please help.

Thanks,
Pushpraj

Hello Pushpraj,

Using only grep will match even a single string too which you are trying to search for, could you please try following
and let us know if this helps you.
1st:

 #su -l zabbix -s /bin/bash -c "nodetool cfstats tests" | grep -i "Memtable switch count"
 

2nd:

 #su -l zabbix -s /bin/bash -c "nodetool cfstats tests" |awk -F":" -vvar="Memtable switch count" '{if($1 == var){print}}'
 

Thanks,
R. Singh

Escape the double quotes around the search pattern.

BTW, I'd be surprised if the result of the first command were "12" only...

Thanks RudiC , You are correct. That was typing mistake. I have updated post.

Soooo, problem solved?

Hi RudiC , I just tried it now. If I run the command as zabbix user by escaping double quotes it ran correctly.

su -l zabbix -s /bin/bash -c "nodetool cfstats cassdb.tests | grep \"Memtable switch count\""
Memtable data size: 12

But problem is this is not working from bash script. Below is the script I am trying.

#!/bin/bash
KEYSPACE="$1"
ATTRIBUTE="$2"
nodetool cfstats cassdb.${KEYSPACE} | /bin/grep "$ATTRIBUTE" | awk -F: '{print $2}' | sed -e 's/^[ \t]*//'

When I run above script as root user it works perfectly.

#/opt/zabbix/cassandra.sh tests "Memtable switch count"
12

Where as when executed as zabbix user it dosent works.

su -l zabbix -s /bin/bash -c "/opt/zabbix/cassandra.sh tests "Memtable switch count""
109
0
105
12

Here 12 is printed as bottom along-with other attributes values which has "Memtable" string in it.

I tried escaping double quotes from bash script but somehow not able to figure out exact syntax.

Thanks RavinderSingh, I tried both the options which you provided but they are not working. No output for the command.

Thanks,
Pushpraj

Why don't you do it exactly as above?

Actually I can not pass attribute "Memtable switch count" directly in script because there are many such attributes which will be passed to script and script will provide the values through the command entered in script.

For example I can pass below attributes as variable to grep
Memtable cell count
Memtable data size
Memtable off heap memory used
Memtable switch count
And many more

So next time I might be using script with different attribute

su -l zabbix -s /bin/bash -c "/opt/zabbix/cassandra.sh tests "Memtable off heap memory used""

Purpose of this is basically zabbix will be making call to this script and output data will be used for monitoring. I will be passing attributes to zabbix through its web interface and zabbix will execute this script for passed attribute.

Hope that clarifies now :slight_smile:

Thanks for your help.

The most likely explanation is that the non root user does not have either permission or authority to run the command.

Below is the suders configuration for zabbix user.

Defaults:zabbix !requiretty
Cmnd_Alias ZABBIX_CMDS = /opt/zabbix/, /opt/zabbix/cassandra.sh, /bin/grep, /usr/bin/nodetool, /usr/bin/awk, /bin/sed
zabbix ALL = NOPASSWD: ZABBIX_CMDS

These are the commands scripts refers to. Am I missing anything here.?

Thanks,
Pushpraj

That's how the command is parsed. The double quotes are not doing what you think.

su -l zabbix -s /bin/bash -c '/opt/zabbix/cassandra.sh tests "Memtable off heap memory used"'