There are plenty of software's to monitor and control Apache, Nginx, SAMBA fileserver etc. But there is one special need that pushed me to search for this.
Problem
I have been using a small precompiled binary on Ubuntu EC2 instance for an specific purpose. I have that binary as a service which starts as Ubuntu starts and accessible by systemctl or the command service start stop status.
Now i have the process running smooth but i want to monitor it and the second requirement is to add new users to the configuration file. Both of these jobs need to be done via web interface.
Step 1:
Choosing a platform, from the beginning i choosed PHP over any other language, in 2006 for my MSc Project automation over internet i tried 3 languages but atlast PHP worked. This time too i choose PHP out of options NodeJS, Python, PHP .
Step 2
started with simplest code. $output=shell_exec('ls'); and then print the variable $output via echo $output . Now this worked very smoothly.
now for the specific process i use a long command with root access
"sudo journalctl -u nx -o cat --since today"
when i put this in my php file nothing appeared
This command in normal terminal takes time so i thought it may be skipping it because of timeout.. but then it should throw a timeout error but there is no timeout error. Later i found people used the same command for remote unzip via web which may take more time than my process. so if unzip working then mine should work.
Yes some people complained the unzip working good except that it does not prompt anything untill whole process finished, so he asked whether any tweak available to get status of unzip as unpacks one by one similar to unzip in direct terminal. Yes some other people suggetsed a way out although thats not necessary still am quoting that via two links Stackoverflow and phpwebsite . Both of these codes are 80% similar they use proc_open() . proc_open() is superior to shell_exec() .
Now coming back to my problem, the function was not throwing any error when i tried with random text as command in shell_exec arguments. Google search suggested me to add few extra characters to get errors on webpage from terminal.
Exmpl: $output = shell_exec('random-non-command 2>&1');
the last few characters are key to error output, " 2>&1 " i dont know the meaning but this works.
well now i got the error, permission denied only sudoers have the eligibility run journalctl
Step 2
well the terminal i use via ssh is a sudoer but when i am executing it via web, its not the same, its www-data (this can be changed via nginx /apache configuration). if you want to check the user, then run
shell_exec('whoami') via the php page
the reply will be www-data
So now add www-data to sudoer with super user command access without password. So now i opened the sudoer file
sudo nano /etc/sudoer
added one line at the base of the file
www-data ALL=NOPASSWD: /bin/journalctl
now the user www-data(nginx) has acccess to journalctl
Complete reading journalctl
Now i restarted nginx once, i dont know whether its necessary or not. Now the webpage showing all events from that process from today morning.
Next
next in my tasks is to read a configuration files to show current status and present that to adminuser and delete append lines as necessary.
again i use the same function shell_exec(cat filename) to print config file details on webpage. i am confused whether i should print the file directly to clients browser or present it the file directly or via some object.

Comments
Post a Comment