发布时间:2019-09-25 08:15:48编辑:auto阅读(1692)
磁盘使用情况
root@ubuntu:/data/server/spider/面试题# cat 02check_disk.py
import time
import os
new_time = time.strftime('%Y-%m-%d')
print(new_time)
disk_status = os.popen('df -h').readlines()
str1 =''.join(disk_status)
with open(new_time+'.log','w') as f:
f.write('%s' % str1)
f.flush()
f.close()
root@ubuntu:/data/server/spider/面试题# cat 2017-11-21.log
Filesystem Size Used Avail Use% Mounted on
udev 1.5G 0 1.5G 0% /dev
tmpfs 300M 9.3M 290M 4% /run
/dev/sda1 21G 13G 7.5G 62% /
tmpfs 1.5G 248K 1.5G 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 1.5G 0 1.5G 0% /sys/fs/cgroup
/dev/sdb1 30G 3.1G 25G 11% /data
tmpfs 300M 0 300M 0% /run/user/0
tmpfs 300M 0 300M 0% /run/user/1000
四、生成磁盘使用情况的日志文件
#!/usr/bin/env python
#!coding=utf-8
import time
import os
new_time = time.strftime('%Y-%m-%d')
disk_status = os.popen('df -h').readlines()
str1 = ''.join(disk_status)
f = file(new_time+'.log','w')
f.write('%s' % str1)
f.flush()
f.close()
五、统计出每个IP的访问量有多少?(从日志文件中查找)
#!/usr/bin/env python
#!coding=utf-8
list = []
f = file('/tmp/1.log')
str1 = f.readlines()
f.close()
for i in str1:
ip = i.split()[0]
list.append(ip)
list_num = set(list)
for j in list_num:
num = list.count(j)
print '%s : %s' %(j,num)
47768
46278
37155
34658
29248
25909
24781
19883
19444
17938
5734°
6339°
5856°
5905°
7009°
5844°
5865°
6375°
6335°
7701°