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.

258 lines
5.4 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
  1. import socket, platform, os, multiprocessing, json
  2. from datetime import timedelta
  3. from django.contrib.auth.decorators import login_required
  4. from django.core import serializers
  5. from django.shortcuts import render_to_response
  6. from django.http import HttpResponseRedirect, HttpResponse
  7. from django.template import RequestContext
  8. from django.utils.translation import ugettext_lazy as _
  9. from pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG, TIME_JS_REFRESH_NET, VERSION
  10. time_refresh = TIME_JS_REFRESH
  11. time_refresh_long = TIME_JS_REFRESH_LONG
  12. time_refresh_net = TIME_JS_REFRESH_NET
  13. version = VERSION
  14. @login_required(login_url='/login/')
  15. def index(request):
  16. """
  17. Index page.
  18. """
  19. return HttpResponseRedirect('/main')
  20. def chunks(get, n):
  21. return [get[i:i+n] for i in range(0, len(get), n)]
  22. def get_uptime():
  23. """
  24. Get uptime
  25. """
  26. try:
  27. with open('/proc/uptime', 'r') as f:
  28. uptime_seconds = float(f.readline().split()[0])
  29. uptime_time = str(timedelta(seconds = uptime_seconds))
  30. data = uptime_time.split('.', 1)[0]
  31. except Exception,err:
  32. data = str(err)
  33. return data
  34. def get_hostname():
  35. """
  36. Get the hostname
  37. """
  38. try:
  39. data = socket.gethostname()
  40. except Exception,err:
  41. data = str(err)
  42. return data
  43. def get_ipaddress():
  44. """
  45. Get the IP Address
  46. """
  47. try:
  48. pipe = os.popen(" ip addr | grep -A3 'state UP' | awk '{printf \"%s,\",$2}'|awk -F, '{print $1, $2, $3}'")
  49. data = pipe.read().strip().split('\n')
  50. pipe.close()
  51. data = [i.split(None, 3) for i in data]
  52. for e in data:
  53. if len(e) > 3:
  54. itf = dict(zip([iter(e[0].strip(':'))]))
  55. else:
  56. itf = [e[0].strip(':')]
  57. ips = {'interface': itf, 'itfip': data}
  58. data = ips
  59. except Exception,err:
  60. data = str(err)
  61. return data
  62. def get_cpus():
  63. """
  64. Get the number of CPUs and model/type
  65. """
  66. try:
  67. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'")
  68. data = pipe.read().strip().split(':')[-1]
  69. pipe.close()
  70. if not data:
  71. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'Processor'")
  72. data = pipe.read().strip().split(':')[-1]
  73. pipe.close()
  74. cpus = multiprocessing.cpu_count()
  75. data = {'cpus': cpus, 'type': data}
  76. except Exception, err:
  77. data = str(err)
  78. return data
  79. def get_users():
  80. """
  81. Get the current logged in users
  82. """
  83. try:
  84. pipe = os.popen("who |" + "awk '{print $1, $2, $6}'")
  85. data = pipe.read().strip().split('\n')
  86. pipe.close()
  87. data = [i.split(None, 3) for i in data]
  88. except Exception, err:
  89. data = str(err)
  90. if data[0] == []:
  91. data = [['No', 'data', 'available']]
  92. return data
  93. def get_traffic(request):
  94. """
  95. Get the traffic for the specified interface
  96. """
  97. try:
  98. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'")
  99. data = pipe.read().strip().split(':',1)[-1]
  100. pipe.close()
  101. if data == ' 0':
  102. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'")
  103. data = pipe.read().strip().split(':',1)[-1]
  104. pipe.close()
  105. data = data.split()
  106. traffic_in = int(data[0])
  107. traffic_out = int(data[1])
  108. all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
  109. data = all_traffic
  110. except Exception,err:
  111. data = str(err)
  112. return data
  113. def get_platform():
  114. """
  115. Get the OS name
  116. """
  117. try:
  118. data = " ".join(platform.linux_distribution())
  119. except Exception,err:
  120. data = str(err)
  121. return data
  122. def get_disk():
  123. """
  124. Get disk usage
  125. """
  126. try:
  127. pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'")
  128. data = pipe.read().strip().split('\n')
  129. pipe.close()
  130. data = [i.split(None, 6) for i in data]
  131. except Exception,err:
  132. data = str(err)
  133. return data
  134. def get_mem():
  135. """
  136. Get memory usage
  137. """
  138. try:
  139. pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'")
  140. data = pipe.read().strip().split()
  141. pipe.close()
  142. allmem = int(data[0])
  143. freemem = int(data[1])
  144. percent = (100 - ((freemem * 100) / allmem))
  145. usage = (allmem - freemem)
  146. mem_usage = {'usage': usage, 'percent': percent}
  147. data = mem_usage
  148. except Exception,err:
  149. data = str(err)
  150. return data
  151. def get_cpu_usage():
  152. """
  153. Get the CPU usage and running processes
  154. """
  155. try:
  156. pipe = os.popen("ps aux --sort -%cpu,-rss")
  157. data = pipe.read().strip().split('\n')
  158. pipe.close()
  159. usage = [i.split(None, 10) for i in data]
  160. del usage[0]
  161. total_usage = []
  162. for element in usage:
  163. usage_cpu = element[2]
  164. total_usage.append(usage_cpu)
  165. total_usage = sum(float(i) for i in total_usage)
  166. total_free = ((100 * int(get_cpus()['cpus'])) - float(total_usage))
  167. cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage}
  168. data = cpu_used
  169. except Exception,err:
  170. data = str(err)
  171. return data
  172. def get_load():
  173. """
  174. Get load average
  175. """
  176. try:
  177. data = os.getloadavg()[0]
  178. except Exception, err:
  179. data = str(err)
  180. return data
  181. @login_required(login_url='/login/')
  182. def getall(request):
  183. return render_to_response('main.html', {'gethostname': get_hostname(),
  184. 'getplatform': get_platform(),
  185. 'getcpus': get_cpus(),
  186. 'time_refresh': time_refresh,
  187. 'time_refresh_long': time_refresh_long,
  188. 'time_refresh_net': time_refresh_net,
  189. 'version': version
  190. }, context_instance=RequestContext(request))