Skip to main content

Server admin tool with php

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

Popular posts from this blog

Questions

1. What is a Stub function? Ans : A function without any definition which presents no error when called. 2. Why there are two ld scripts generated by STM32Cube IDE? Ans: The CubeIDE always presents two different LDscripts on for generating the executable for Debugging which goes to RAM another for normal code upload that goes to flash memory. 3. What is Supervisory Mode/ privileged mode ? Ans : When you are using a Kernel then there are two modes user mode and privileged mode. In User mode the applications cant have system calls. 4. What is "make -j6" ? Ans : Make has an argument for number of jobs, so when you add -j6 it will create 6 different compiler instances, so that the systems could use multicore to the fullest.  Linux people generally do add "-j $(nproc)" where nproc is a command line which returns number of processors attached. 5. What is weak attribute ? Ans : Weak attribute used to denote weak symbols which helps linkers to choose one function out of mul...

#@$%

  #include  "msp430x21x2.h"     #define PWM_OUTPUT              0x03 #define DUTY_CYCLE              TA0CCR2 #define MAX_LIMIT               300   /* #define no_of_samples           1 #define duty_cycle_limit        1000 #define temp_sensor             0xA000   #define panel_selection_cutoff  250 */ #define PV_VOLTAGE              INCH_0 #define PV_CURRENT              INCH_1 #define BATT_VOLTAGE            INCH_2 #define BATT_CURRENT            INCH_3   #define UPPER_CRG_LVL           0x0105 // 0x00FF // 0x00F5 //0x023A #define LOWER_CRG_LVL   ...

Preparing for embedded systems interview

  Preparing for a technical interview in embedded systems involves a wide range of topics, as the field encompasses both hardware and software components. Here are some key areas and example questions that you might encounter in such an interview: Basic Concepts and Theory: What is an embedded system? Can you explain the difference between a microprocessor and a microcontroller? Describe the various types of memory in an embedded system. Programming and Software Design: 4. How do you write an interrupt service routine in C? 5. Explain the concept of a real-time operating system (RTOS). How does it differ from a general-purpose operating system? 6. What are the different states of a thread in an RTOS? Hardware and Interfacing : 7. How does SPI communication work? What about I2C communication? 8. Explain the concept of GPIO (General-Purpose Input/Output). How would you use it in an embedded application? 9. What are interrupts, and how are they handled in embedded systems? Low-Level P...