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.

304 lines
7.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
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 socket, 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 'state UP' | awk '{printf \"%s,\",$2}'|awk -F, '{print $1, $2, $3}'")
  61. data = pipe.read().strip().split('\n')
  62. pipe.close()
  63. data = [i.split(None, 3) for i in data]
  64. for e in data:
  65. if len(e) > 3:
  66. itf = dict(zip([iter(e[0].strip(':'))]))
  67. else:
  68. itf = [e[0].strip(':')]
  69. ips = {'interface': itf, 'itfip': data}
  70. data = ips
  71. except Exception,err:
  72. data = str(err)
  73. return data
  74. def get_cpus():
  75. """
  76. Get the number of CPUs and model/type
  77. """
  78. try:
  79. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'")
  80. data = pipe.read().strip().split(':')[-1]
  81. pipe.close()
  82. if not data:
  83. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'Processor'")
  84. data = pipe.read().strip().split(':')[-1]
  85. pipe.close()
  86. cpus = multiprocessing.cpu_count()
  87. data = {'cpus': cpus, 'type': data}
  88. except Exception, err:
  89. data = str(err)
  90. return data
  91. def get_users():
  92. """
  93. Get the current logged in users
  94. """
  95. try:
  96. pipe = os.popen("who |" + "awk '{print $1, $2, $6}'")
  97. data = pipe.read().strip().split('\n')
  98. pipe.close()
  99. data = [i.split(None, 3) for i in data]
  100. except Exception, err:
  101. data = str(err)
  102. if data[0] == []:
  103. data = [['No', 'data', 'available']]
  104. return data
  105. def get_traffic(request):
  106. """
  107. Get the traffic for the specified interface
  108. """
  109. try:
  110. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'")
  111. data = pipe.read().strip().split(':',1)[-1]
  112. pipe.close()
  113. if data == ' 0':
  114. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'")
  115. data = pipe.read().strip().split(':',1)[-1]
  116. pipe.close()
  117. data = data.split()
  118. traffic_in = int(data[0])
  119. traffic_out = int(data[1])
  120. all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
  121. data = all_traffic
  122. except Exception,err:
  123. data = str(err)
  124. return data
  125. def get_platform():
  126. """
  127. Get the OS name, hostname and kernel
  128. """
  129. try:
  130. osname = " ".join(platform.linux_distribution())
  131. uname = platform.uname()
  132. data = {'osname': osname, 'hostname': uname[1], 'kernel': uname[2] }
  133. except Exception,err:
  134. data = str(err)
  135. return data
  136. def get_disk():
  137. """
  138. Get disk usage
  139. """
  140. try:
  141. pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'")
  142. data = pipe.read().strip().split('\n')
  143. pipe.close()
  144. data = [i.split(None, 6) for i in data]
  145. except Exception,err:
  146. data = str(err)
  147. return data
  148. def get_disk_rw():
  149. """
  150. Get the disk reads and writes
  151. """
  152. try:
  153. pipe = os.popen("cat /proc/partitions | grep -v 'major' | awk '{print $4}'")
  154. data = pipe.read().strip().split('\n')
  155. pipe.close()
  156. rws = []
  157. for i in data:
  158. if i.isalpha():
  159. pipe = os.popen("cat /proc/diskstats | grep -w '" + i + "'|awk '{print $4, $8}'")
  160. rw = pipe.read().strip().split()
  161. pipe.close()
  162. rws.append([i, rw[0], rw[1]])
  163. if not rws:
  164. pipe = os.popen("cat /proc/diskstats | grep -w '" + data[0] + "'|awk '{print $4, $8}'")
  165. rw = pipe.read().strip().split()
  166. pipe.close()
  167. rws.append([data[0], rw[0], rw[1]])
  168. data = rws
  169. except Exception,err:
  170. data = str(err)
  171. return data
  172. def get_mem():
  173. """
  174. Get memory usage
  175. """
  176. try:
  177. pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'")
  178. data = pipe.read().strip().split()
  179. pipe.close()
  180. allmem = int(data[0])
  181. freemem = int(data[1])
  182. percent = (100 - ((freemem * 100) / allmem))
  183. usage = (allmem - freemem)
  184. mem_usage = {'usage': usage, 'percent': percent}
  185. data = mem_usage
  186. except Exception,err:
  187. data = str(err)
  188. return data
  189. def get_cpu_usage():
  190. """
  191. Get the CPU usage and running processes
  192. """
  193. try:
  194. pipe = os.popen("ps aux --sort -%cpu,-rss")
  195. data = pipe.read().strip().split('\n')
  196. pipe.close()
  197. usage = [i.split(None, 10) for i in data]
  198. del usage[0]
  199. total_usage = []
  200. for element in usage:
  201. usage_cpu = element[2]
  202. total_usage.append(usage_cpu)
  203. total_usage = sum(float(i) for i in total_usage)
  204. total_free = ((100 * int(get_cpus()['cpus'])) - float(total_usage))
  205. cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage}
  206. data = cpu_used
  207. except Exception,err:
  208. data = str(err)
  209. return data
  210. def get_load():
  211. """
  212. Get load average
  213. """
  214. try:
  215. data = os.getloadavg()[0]
  216. except Exception, err:
  217. data = str(err)
  218. return data
  219. @login_required(login_url='/login/')
  220. def getall(request):
  221. return render_to_response('main.html', {'gethostname': get_platform()['hostname'],
  222. 'getplatform': get_platform()['osname'],
  223. 'getkernel': get_platform()['kernel'],
  224. 'getcpus': get_cpus(),
  225. 'time_refresh': time_refresh,
  226. 'time_refresh_long': time_refresh_long,
  227. 'time_refresh_net': time_refresh_net,
  228. 'version': version
  229. }, context_instance=RequestContext(request))