Ajout rendus TP

This commit is contained in:
wpetit 2017-12-08 11:34:34 +01:00 committed by Benjamin Bohard
parent fc21384b39
commit 0bcbd982cb
13 changed files with 174 additions and 0 deletions

1
diiage/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.~lock*

View File

@ -0,0 +1,86 @@
# TD Rest Api Python
## Requirements
* Python 2.7 (tested on 2.7.13)
* PIP (tested on 9.0.1)
## Installation
$ git clone git@github.com:clementtournier/td-python-rest.git
$ cd td-python-rest
$ git checkout develop
$ pip install -r requirements.txt
$ python server.py
Now the server is listening on 127.0.0.1:5000
## API endpoints
### GET /disk_usage
```
[
{
"available": "3.9G",
"filesystem": "udev",
"mountpoint": "/dev",
"size": "3.9G",
"used": "0",
"used_percent": "0%"
},
{
"available": "778M",
"filesystem": "tmpfs",
"mountpoint": "/run",
"size": "787M",
"used": "9.8M",
"used_percent": "2%"
},
...
]
```
#### SAMPLE CALL
````
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:5000/disk_usage
````
### GET /processes
```
[
{
"c": "0",
"cmd": "/sbin/init",
"pid": "1",
"ppid": "0",
"stime": "17:26",
"time": "00:00:01",
"tty": "?",
"uid": "root"
},
{
"c": "0",
"cmd": "[ksoftirqd/0]",
"pid": "3",
"ppid": "2",
"stime": "17:26",
"time": "00:00:00",
"tty": "?",
"uid": "root"
},
...
]
```
### POST /processes
```
POST /processes HTTP/1.1
Host: 127.0.0.1:5000
Cache-Control: no-cache
Postman-Token: 48a0ea07-b1e6-f0a1-184b-fe23c7172b91
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="process"
apache2
```
return True is service is stopped or False.

View File

@ -0,0 +1,82 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, request
from flask_restful import Resource, Api
import os
import json
app = Flask(__name__)
api = Api(app)
class DiskUsage(Resource):
def df_h(self):
df = []
dfCmd = os.popen("df -h ")
i = 0
while True:
line = dfCmd.readline()
if line != '':
i = i + 1
if i > 1: #Suppression de la première ligne du df -h
lineDatas = line.split()[0:6] #Pour parser le fichier
df.append({ #Les attributs d'un objet pour le JSON de fin
"filesystem" : lineDatas[0],
"size" : lineDatas[1],
"used" : lineDatas[2],
"available" : lineDatas[3],
"used_percent" : lineDatas[4],
"mountpoint" : lineDatas[5],
})
else:
break
return df
def get(self):
return self.df_h()
class Processes(Resource):
def listProcesses(self):
content = []
statusCmd = os.popen("ps -ef")
i = 0
while True:
line = statusCmd.readline()
if line != '':
i = i + 1
if i > 1:
lineDatas = line.split()[0:8]
content.append({
'uid': lineDatas[0],
'pid': lineDatas[1],
'ppid': lineDatas[2],
'c': lineDatas[3],
'stime': lineDatas[4],
'tty': lineDatas[5],
'time': lineDatas[6],
'cmd': lineDatas[7]
})
else:
break
return content
def killService(self):
data = request.form
if 'process' in data:
os.popen("systemctl stop " + data['process'])
return { True }
return { False }
def get(self):
return self.listProcesses()
def post(self):
return self.killService()
api.add_resource(DiskUsage, '/disk_usage')
api.add_resource(Processes, '/processes')
if __name__ == '__main__':
app.run(debug=True)