Pages

Friday, June 23, 2017

Python code to monitor conversion process or any process in Linux

Python code to monitor the conversion process 



* Script will monitor the process for specific time after that it will kill it

* it will print the CPU%,Mem%

* First it will get the container PID , then the child PID 


Method Example :



 1


 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def runOITprocess(timeout):
   commandtouse="cd /opt/app/oit ;./exsimple myfile.docx  output.pdf >  log.txt"  ## conversion process 
   proc=subprocess.Popen(commandtouse, shell=True)
   motherProcess=proc.pid    ## Get Mother process , This process of new shell not real one 
   pidtomonitor="ps -ef | grep "+ str (motherProcess)+" | grep exsimple | grep -v '/bin/sh' | awk '{print $2}'" 
   findingChild = subprocess.Popen(pidtomonitor, shell=True,stdout=subprocess.PIPE)
   childProcess = (findingChild.stdout.read().strip())
   print childProcess  ## This is real process 
   count=0
   awkstring='{print $6" "$9" " $10}'
   fromtopcommand="top -b -n 1 -p "+  (childProcess)+" | tail -1 | awk '"+awkstring+"'"  ## To get Memory%,CPU%
   timedOut=False
   detailsofProces=[]
   while True:
     #print proc.poll()
     if  proc.poll() is not None:
         break
     topcommand  = subprocess.Popen(fromtopcommand, shell=True,stdout=subprocess.PIPE)
     detailsofProces.append(topcommand.stdout.read())
     time.sleep(1)
     stdout.write('\r Running for ' + str (count)  +" s")
     stdout.flush()
     count=count+1
     if timeout==count:
         os.system("kill -9 "+  (childProcess))           ## Killing the child process when time out is over 
         print "Killed due to time out"
         timedOut=True

   CPUL=[]
   MemL=[]
   ResL=[]
   for kk in detailsofProces:
       details=kk.split()
       if details[0].startswith("RES"):
           continue
       ResL.append(details[0])
       CPUL.append(details[1])
       MemL.append(details[2].strip())
   created=False
   if  os.path.isfile("/opt/app/oit/output.pdf"):
       created=True
   distProcessStatus={"timedOut":timedOut,"Time":count,"CPU":CPUL,"Mem":MemL,"Res":ResL,"created":created}
   return distProcessStatus


Example :

[sterin@sterin-linux oittest]$ ./oitUtilis.py
9988
 Running for 7 s{'created': True, 'Mem': ['0.1', '0.2', '0.7', '2.7', '3.1', '3.4', '3.8', '3.8'], 'Res': ['11188', '31964', '107064', '427708', '484072', '536436', '607600', '600400'], 'timedOut': False, 'Time': 8, 'CPU': ['6.7', '13.3', '100.0', '100.0', '100.0', '100.0', '100.0', '100.0']}




Code is highlighted using : http://hilite.me/

Thursday, May 18, 2017

Flask with two way SSL



Flask with two way SSL


For two way SSL :




For Flask in Apache using WSGI


1. Edit WSGI virtual host


Add :
RequestHeader set SSL_CLIENT_S_DN "%{SSL_CLIENT_S_DN}s"


WSGISocketPrefix /var/run/wsgi

<VirtualHost _default_:443>
ServerName st.company.com
WSGIDaemonProcess yourapplication user=apache group=apache threads=5
WSGIScriptAlias /cotton /var/www/flask/cotton/myapp.wgi
RequestHeader set SSL_CLIENT_S_DN "%{SSL_CLIENT_S_DN}s"
 <Directory /var/www/flask/cotton/>
WSGIProcessGroup yourapplication
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
      
this will provide in SSL DN details in request.environ



'HTTP_SSL_CLIENT_S_DN': '/C=IN/L=Bangalore/O=EcmTest Vms/CN=Sterin'




request.headers.get('SSL_CLIENT_S_DN_CN') wont work properly



2. Add login in flask py file





@app.route('/ServerStatus')

def ServerStatus():
     user=""
    s_dn= (request.environ.get('HTTP_SSL_CLIENT_S_DN'))
    if s_dn:
          user = dict([x.split('=') for x in s_dn.split('/')[1:]])['CN']
    if user is not None and user != '':
      return render_template('login.html',user=user)
    else:
      return redirect('/')

Monday, May 15, 2017

Installing gnome extension addon on linux




Installing gnome extension addon on linux

gnome extension is required to new install all 
GNOME 3 extension to control UI of GNOME 

1. Install git



    yum install git


2. Install cmake



    yum install cmake 


3. Install python ,python-requests



    yum install python python-requests




4. Install jq



    yum install jq




5. Clone git jq



    git clone git://git.gnome.org/chrome-gnome-shell






5. build



    cd chrome-gnome-shell/
    mkdir build
    cd build
    cmake -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_EXTENSION=OFF ../
    make install


Final screen :





















This will fix below errors :




1. "Native host has exited."

2. No extension