You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
7.8 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. #The MIT License (MIT)
  2. #
  3. #Copyright (c) 2014 Florian Neagu - michaelneagu@gmail.com - https://github.com/k3oni/
  4. #
  5. #Permission is hereby granted, free of charge, to any person obtaining a copy
  6. #of this software and associated documentation files (the "Software"), to deal
  7. #in the Software without restriction, including without limitation the rights
  8. #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. #copies of the Software, and to permit persons to whom the Software is
  10. #furnished to do so, subject to the following conditions:
  11. #
  12. #The above copyright notice and this permission notice shall be included in all
  13. #copies or substantial portions of the Software.
  14. #
  15. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. #SOFTWARE.
  22. import platform, os, multiprocessing, json
  23. from datetime import timedelta
  24. from django.contrib.auth.decorators import login_required
  25. from django.core import serializers
  26. from django.shortcuts import render_to_response
  27. from django.http import HttpResponseRedirect, HttpResponse
  28. from django.template import RequestContext
  29. from django.utils.translation import ugettext_lazy as _
  30. from pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG, TIME_JS_REFRESH_NET, VERSION
  31. time_refresh = TIME_JS_REFRESH
  32. time_refresh_long = TIME_JS_REFRESH_LONG
  33. time_refresh_net = TIME_JS_REFRESH_NET
  34. version = VERSION
  35. @login_required(login_url='/login/')
  36. def index(request):
  37. """
  38. Index page.
  39. """
  40. return HttpResponseRedirect('/main')
  41. def chunks(get, n):
  42. return [get[i:i+n] for i in range(0, len(get), n)]
  43. def get_uptime():
  44. """
  45. Get uptime
  46. """
  47. try:
  48. with open('/proc/uptime', 'r') as f:
  49. uptime_seconds = float(f.readline().split()[0])
  50. uptime_time = str(timedelta(seconds = uptime_seconds))
  51. data = uptime_time.split('.', 1)[0]
  52. except Exception,err:
  53. data = str(err)
  54. return data
  55. def get_ipaddress():
  56. """
  57. Get the IP Address
  58. """
  59. try:
  60. pipe = os.popen(" ip addr | grep -A3 'LOWER_UP' | awk '{printf \"%s,\",$2}'|awk -F,, '{print $1, $2, $3}'")
  61. data = pipe.read().strip().split(' ')
  62. pipe.close()
  63. data = [n for n in data if not n.startswith(('lo', '127'))]
  64. data = [i.split(',', 3) for i in data]
  65. itf = []
  66. for e in data:
  67. itf.append(e[0].strip(':'))
  68. ips = {'interface': itf, 'itfip': data}
  69. data = ips
  70. except Exception,err:
  71. data = str(err)
  72. return data
  73. def get_cpus():
  74. """
  75. Get the number of CPUs and model/type
  76. """
  77. try:
  78. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'")
  79. data = pipe.read().strip().split(':')[-1]
  80. pipe.close()
  81. if not data:
  82. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'Processor'")
  83. data = pipe.read().strip().split(':')[-1]
  84. pipe.close()
  85. cpus = multiprocessing.cpu_count()
  86. data = {'cpus': cpus, 'type': data}
  87. except Exception, err:
  88. data = str(err)
  89. return data
  90. def get_users():
  91. """
  92. Get the current logged in users
  93. """
  94. try:
  95. pipe = os.popen("who |" + "awk '{print $1, $2, $6}'")
  96. data = pipe.read().strip().split('\n')
  97. pipe.close()
  98. data = [i.split(None, 3) for i in data]
  99. except Exception, err:
  100. data = str(err)
  101. if data[0] == []:
  102. data = [['No', 'data', 'available']]
  103. return data
  104. def get_traffic(request):
  105. """
  106. Get the traffic for the specified interface
  107. """
  108. try:
  109. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'")
  110. data = pipe.read().strip().split(':',1)[-1]
  111. pipe.close()
  112. if not data[0].isdigit():
  113. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'")
  114. data = pipe.read().strip().split(':',1)[-1]
  115. pipe.close()
  116. data = data.split()
  117. traffic_in = int(data[0])
  118. traffic_out = int(data[1])
  119. all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
  120. data = all_traffic
  121. except Exception,err:
  122. data = str(err)
  123. return data
  124. def get_platform():
  125. """
  126. Get the OS name, hostname and kernel
  127. """
  128. try:
  129. osname = " ".join(platform.linux_distribution())
  130. uname = platform.uname()
  131. if osname == ' ':
  132. osname = uname[0]
  133. data = {'osname': osname, 'hostname': uname[1], 'kernel': uname[2] }
  134. except Exception,err:
  135. data = str(err)
  136. return data
  137. def get_disk():
  138. """
  139. Get disk usage
  140. """
  141. try:
  142. pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'")
  143. data = pipe.read().strip().split('\n')
  144. pipe.close()
  145. data = [i.split(None, 6) for i in data]
  146. except Exception,err:
  147. data = str(err)
  148. return data
  149. def get_disk_rw():
  150. """
  151. Get the disk reads and writes
  152. """
  153. try:
  154. pipe = os.popen("cat /proc/partitions | grep -v 'major' | awk '{print $4}'")
  155. data = pipe.read().strip().split('\n')
  156. pipe.close()
  157. rws = []
  158. for i in data:
  159. if i.isalpha():
  160. pipe = os.popen("cat /proc/diskstats | grep -w '" + i + "'|awk '{print $4, $8}'")
  161. rw = pipe.read().strip().split()
  162. pipe.close()
  163. rws.append([i, rw[0], rw[1]])
  164. if not rws:
  165. pipe = os.popen("cat /proc/diskstats | grep -w '" + data[0] + "'|awk '{print $4, $8}'")
  166. rw = pipe.read().strip().split()
  167. pipe.close()
  168. rws.append([data[0], rw[0], rw[1]])
  169. data = rws
  170. except Exception,err:
  171. data = str(err)
  172. return data
  173. def get_mem():
  174. """
  175. Get memory usage
  176. """
  177. try:
  178. pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'")
  179. data = pipe.read().strip().split()
  180. pipe.close()
  181. allmem = int(data[0])
  182. freemem = int(data[1])
  183. percent = (100 - ((freemem * 100) / allmem))
  184. usage = (allmem - freemem)
  185. mem_usage = {'usage': usage, 'percent': percent}
  186. data = mem_usage
  187. except Exception,err:
  188. data = str(err)
  189. return data
  190. def get_cpu_usage():
  191. """
  192. Get the CPU usage and running processes
  193. """
  194. try:
  195. pipe = os.popen("ps aux --sort -%cpu,-rss")
  196. data = pipe.read().strip().split('\n')
  197. pipe.close()
  198. usage = [i.split(None, 10) for i in data]
  199. del usage[0]
  200. total_usage = []
  201. for element in usage:
  202. usage_cpu = element[2]
  203. total_usage.append(usage_cpu)
  204. total_usage = sum(float(i) for i in total_usage)
  205. total_free = ((100 * int(get_cpus()['cpus'])) - float(total_usage))
  206. cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage}
  207. data = cpu_used
  208. except Exception,err:
  209. data = str(err)
  210. return data
  211. def get_load():
  212. """
  213. Get load average
  214. """
  215. try:
  216. data = os.getloadavg()[0]
  217. except Exception, err:
  218. data = str(err)
  219. return data
  220. def get_netstat():
  221. """
  222. Get ports and applications
  223. """
  224. try:
  225. pipe = os.popen("netstat -tlnp |" + "grep 'LISTEN' |" + "awk '{print $4, $5, $7}'")
  226. data = pipe.read().strip().split('\n')
  227. pipe.close()
  228. data = [i.split(None, 3) for i in data]
  229. except Exception, err:
  230. data = str(err)
  231. if data[0] == []:
  232. data = [['No', 'data', 'available']]
  233. return data
  234. @login_required(login_url='/login/')
  235. def getall(request):
  236. return render_to_response('main.html', {'gethostname': get_platform()['hostname'],
  237. 'getplatform': get_platform()['osname'],
  238. 'getkernel': get_platform()['kernel'],
  239. 'getcpus': get_cpus(),
  240. 'time_refresh': time_refresh,
  241. 'time_refresh_long': time_refresh_long,
  242. 'time_refresh_net': time_refresh_net,
  243. 'version': version
  244. }, context_instance=RequestContext(request))