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.

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