Commit cd065e5a authored by Sébastien's avatar Sébastien

ajout du nombre d'utilisateurs connectés

parent dac737ef
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
ipython = "*"
[requires]
python_version = "3.8"
{
"_meta": {
"hash": {
"sha256": "82119c477cfe55e8c4a836bc241def1d8a1262f46e8cb2f7d8023067781250f3"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.8"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"backcall": {
"hashes": [
"sha256:38ecd85be2c1e78f77fd91700c76e14667dc21e2713b63876c0eb901196e01e4",
"sha256:bbbf4b1e5cd2bdb08f915895b51081c041bac22394fdfcfdfbe9f14b77c08bf2"
],
"version": "==0.1.0"
},
"decorator": {
"hashes": [
"sha256:41fa54c2a0cc4ba648be4fd43cff00aedf5b9465c9bf18d64325bc225f08f760",
"sha256:e3a62f0520172440ca0dcc823749319382e377f37f140a0b99ef45fecb84bfe7"
],
"version": "==4.4.2"
},
"ipython": {
"hashes": [
"sha256:ca478e52ae1f88da0102360e57e528b92f3ae4316aabac80a2cd7f7ab2efb48a",
"sha256:eb8d075de37f678424527b5ef6ea23f7b80240ca031c2dd6de5879d687a65333"
],
"index": "pypi",
"version": "==7.13.0"
},
"ipython-genutils": {
"hashes": [
"sha256:72dd37233799e619666c9f639a9da83c34013a73e8bbc79a7a6348d93c61fab8",
"sha256:eb2e116e75ecef9d4d228fdc66af54269afa26ab4463042e33785b887c628ba8"
],
"version": "==0.2.0"
},
"jedi": {
"hashes": [
"sha256:b4f4052551025c6b0b0b193b29a6ff7bdb74c52450631206c262aef9f7159ad2",
"sha256:d5c871cb9360b414f981e7072c52c33258d598305280fef91c6cae34739d65d5"
],
"version": "==0.16.0"
},
"parso": {
"hashes": [
"sha256:0c5659e0c6eba20636f99a04f469798dca8da279645ce5c387315b2c23912157",
"sha256:8515fc12cfca6ee3aa59138741fc5624d62340c97e401c74875769948d4f2995"
],
"version": "==0.6.2"
},
"pexpect": {
"hashes": [
"sha256:0b48a55dcb3c05f3329815901ea4fc1537514d6ba867a152b581d69ae3710937",
"sha256:fc65a43959d153d0114afe13997d439c22823a27cefceb5ff35c2178c6784c0c"
],
"markers": "sys_platform != 'win32'",
"version": "==4.8.0"
},
"pickleshare": {
"hashes": [
"sha256:87683d47965c1da65cdacaf31c8441d12b8044cdec9aca500cd78fc2c683afca",
"sha256:9649af414d74d4df115d5d718f82acb59c9d418196b7b4290ed47a12ce62df56"
],
"version": "==0.7.5"
},
"prompt-toolkit": {
"hashes": [
"sha256:563d1a4140b63ff9dd587bda9557cffb2fe73650205ab6f4383092fb882e7dc8",
"sha256:df7e9e63aea609b1da3a65641ceaf5bc7d05e0a04de5bd45d05dbeffbabf9e04"
],
"version": "==3.0.5"
},
"ptyprocess": {
"hashes": [
"sha256:923f299cc5ad920c68f2bc0bc98b75b9f838b93b599941a6b63ddbc2476394c0",
"sha256:d7cc528d76e76342423ca640335bd3633420dc1366f258cb31d05e865ef5ca1f"
],
"version": "==0.6.0"
},
"pygments": {
"hashes": [
"sha256:647344a061c249a3b74e230c739f434d7ea4d8b1d5f3721bc0f3558049b38f44",
"sha256:ff7a40b4860b727ab48fad6360eb351cc1b33cbf9b15a0f689ca5353e9463324"
],
"version": "==2.6.1"
},
"six": {
"hashes": [
"sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a",
"sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c"
],
"version": "==1.14.0"
},
"traitlets": {
"hashes": [
"sha256:70b4c6a1d9019d7b4f6846832288f86998aa3b9207c6821f3578a6a6a467fe44",
"sha256:d023ee369ddd2763310e4c3eae1ff649689440d4ae59d7485eb4cfbbe3e359f7"
],
"version": "==4.3.3"
},
"wcwidth": {
"hashes": [
"sha256:cafe2186b3c009a04067022ce1dcd79cb38d8d65ee4f4791b8888d6599d1bbe1",
"sha256:ee73862862a156bf77ff92b09034fc4825dd3af9cf81bc5b360668d425f3c5f1"
],
"version": "==0.1.9"
}
},
"develop": {}
}
......@@ -4,13 +4,14 @@ En bref
Présente quelques informations simples sur le système d'exploitation:
- distribution
- version
- charge CPU sur 5mn
- nb utlisateurs connectés
Installation
============
Linux
-----
Nécessite la commande **lsb_release**.
$ adduser --system --home /opt/osinfo --no-create-home osinfo
$ cp osinfo.service /etc/systemd/system/
......@@ -19,8 +20,10 @@ $ systemctl enable osinfo
Windows
-------
Nécessite python: https://www.python.org/downloads/windows/
Lancement: pipenv run py server.py
À faire.
Lancement manuel
================
......
#!/usr/bin/env python3
"""
Permet d'avoir quelques informations sur le système:
- le type d'OS
- version de l'OS
- le nombre d'utilisateurs connectés
"""
import argparse
import logging
import platform
import os
import sys
import subprocess
import socket
PORT = 7777
VERSION = "18.01.08"
LOG = logging.getLogger("osinfo")
class CustomFormatter(argparse.RawDescriptionHelpFormatter,
argparse.ArgumentDefaultsHelpFormatter):
pass
def parse_args(args):
"""Parse arguments."""
parser = argparse.ArgumentParser(
description=sys.modules[__name__].__doc__,
formatter_class=CustomFormatter)
g = parser.add_mutually_exclusive_group()
g.add_argument("--debug", "-d", action="store_true",
default=False,
help="enable debugging")
g.add_argument("--silent", "-s", action="store_true",
default=False,
help="don't log to console")
return parser.parse_args(args)
def setup_logging(options):
"""Configure logging."""
root = logging.getLogger("osinfo")
root.setLevel(logging.WARNING)
LOG.setLevel(options.debug and logging.DEBUG or logging.INFO)
if not options.silent:
if not sys.stderr.isatty():
facility = logging.handlers.SysLogHandler.LOG_DAEMON
sh = logging.handlers.SysLogHandler(address='/dev/log',
facility=facility)
sh.setFormatter(logging.Formatter(
"{0}[{1}]: %(message)s".format(
LOG.name,
os.getpid())))
root.addHandler(sh)
else:
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter(
"%(levelname)s[%(name)s] %(message)s"))
root.addHandler(ch)
def main(host="0.0.0.0", port=7777):
"""Va retourner les informations suivantes sur une même ligne,
en utilisant un ';' comme séparateur.
On a mis de côté les infos suivantes car non multiplateforme par défaut
- nombre d'utilisateur connecté
- uptime
Exemple:
os;os_version;charge
os;os_version;nb_users
"""
mySocket = socket.socket()
mySocket.bind((host,port))
print("starting OSInfo [%s] ..." % VERSION)
mySocket.listen(1)
while True:
conn, addr = mySocket.accept()
print("connection from: " + str(addr))
LOG.debug("connection from: " + str(addr))
data = conn.recv(1024).decode()
#if not data:
# break
print("received from client: " + str(data))
# make data
os_name = "inconnue"
os_version = platform.version()
os_load = "inconnue"
LOG.debug("received from client: " + str(data))
system = platform.system()
if system == 'Linux':
# Linux Fedora 26
# Linux Ubuntu 17.04
# distrib = ('Fedora', '26', 'Twenty Six')
distrib = platform.dist()
os_name = "Linux %s" % " ".join(distrib[:2])
out = subprocess.run(["/usr/bin/lsb_release", "-ds"], stdout=subprocess.PIPE)
os_name = out.stdout.decode().replace('\n', '')
os_version = get_os_version()
os_load = get_load()
os_users = get_users_linux()
elif system == 'Windows':
# Windows 10
os_name = "%s %s" % (system, platform.release())
os_version = platform.version()
os_users = get_users_windows()
message = "%s;%s;%s" % (os_name, os_version, os_load)
message = "%s;%s;%s" % (os_name, os_version, os_users)
print("sending: " + str(message))
LOG.debug("sending: " + str(message))
conn.send(message.encode())
conn.close()
def get_os_version():
"Cas des postes pédago"
file_version = "/etc/version"
......@@ -63,7 +95,7 @@ def get_os_version():
fd = open(file_version, 'r')
tmp = fd.readline().split('\n')[0]
except Exception:
version = 'inconnue'
version = '?'
else:
version = tmp
return version
......@@ -72,20 +104,34 @@ def get_load():
try:
load = os.getloadavg()[1]
except Exception:
load = "inconnue"
load = "?"
return load
def get_users_linux():
try:
# seulement version > 3.7
# out = subprocess.run(["users"], capture_output=True)
out = subprocess.run(["users"], stdout=subprocess.PIPE)
txt = out.stdout.decode().split()
# txt: ['root'] ou ['root', 'root'] ou ['']
nb = len(txt)
if nb == 1 and txt[0] == '':
users = "0"
else:
users = f"{nb}"
except Exception:
users = "?"
return users
def usage():
print('Usage: la_commande_sans_argument')
def get_users_windows():
# commande "quser" ?
return "?"
if __name__ == "__main__":
from sys import argv
if len(argv) == 1:
# run()
# run(port=int(argv[1]))
options = parse_args(sys.argv[1:])
setup_logging(options)
LOG.info("starting...")
main()
else:
usage()
sys.exit()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment