Skip to content

Commit

Permalink
echarts and search
Browse files Browse the repository at this point in the history
  • Loading branch information
YoLoveLife committed Aug 28, 2018
1 parent 227ff54 commit 7f33106
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 85 deletions.
6 changes: 4 additions & 2 deletions apps/authority/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def status(self):
return self._status

@status.setter
def status(self,status):
def status(self, status):
self.check_status()

def check_status(self):
Expand All @@ -207,4 +207,6 @@ def to_yaml(self):
KEY='{{KEY}}'
)
}
}
}


4 changes: 4 additions & 0 deletions apps/authority/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def on_failure(self, exc, task_id, args, kwargs, einfo):

@task(base=JumperTask)
def jumper_status_flush(obj):
import time
print('waiting')
time.sleep(5)
print('done')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(settings.SSH_TIMEOUT)
try:
Expand Down
15 changes: 12 additions & 3 deletions apps/ezsetup/ansible_v2/play_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
'hosts': None,
'remote_user': 'root',
'gather_facts': 'no',
'roles': [
'{{ROLE}}'
]
'tasks': [
{
'script': '{{TOOL}}/yotelnet 127.0.0.1 {{ REDIS_PORT }}',
'register': 'ALREADY_INSTALL'
}
],
# 'roles': [
# {
# 'role': '{{ROLE}}',
# 'when': 'ALREADY_INSTALL!=False',
# }
# ]
},]
8 changes: 1 addition & 7 deletions apps/ezsetup/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
# Email [email protected]

from __future__ import absolute_import, unicode_literals
from celery.task import periodic_task
from celery.schedules import crontab
from django.db.models import Q
import celery
import MySQLdb
from django.conf import settings
import socket
import os,stat,time
from ezsetup.ansible_v2.callback import EZSetupCallback
from ezsetup.ansible_v2.playbook import EZSetupPlaybook
Expand Down Expand Up @@ -67,7 +61,7 @@ def install_redis(instance, detail):
callback = EZSetupCallback(instance)
ezsetup = EZSetupPlaybook(instance.group, KEY, callback, instance)
ezsetup.import_vars(vars_dict)
from ezsetup.ansible.play_source import PLAY_SOURCE
from ezsetup.ansible_v2.play_source import PLAY_SOURCE
# print(list(instance.hosts.filter(_status=settings.STATUS_HOST_CAN_BE_USE).values_list('connect_ip', flat=True)))
PLAY_SOURCE[0]['hosts'] = list(instance.hosts.filter(_status=settings.STATUS_HOST_CAN_BE_USE).values_list('connect_ip', flat=True))
ezsetup.import_task(PLAY_SOURCE)
Expand Down
2 changes: 2 additions & 0 deletions apps/manager/ansible_v2/play_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
},
{
'shell': 'df -hP|grep \/$ |awk \'{print $5}\''
},{
'shell': 'df -hiP|grep \/$ |awk \'{print $5}\''
}
]
},]
Expand Down
13 changes: 5 additions & 8 deletions apps/monitor/api/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def time_pick(obj, time):
else:
obj.request_to_hour(obj, 1)


class MonitorHostAliyunDetailCPUAPI(WebTokenAuthentication, APIView):
permission_classes = [MonitorPermission.MonitorAliyunAPIRequiredMixin, IsAuthenticated]

Expand All @@ -52,8 +53,7 @@ def get(self, request, *args, **kwargs):
time_pick(API, time)
API.request_to_instance(API,self.get_object().aliyun_id)
API.get_cpu_results()
results = API.get_line_opts(API.get_results(), 'CPU使用率')
return Response(results, status.HTTP_200_OK)
return Response({'title':'CPU利用率', 'dataset': API.change_timestamp(API.get_results())})


class MonitorHostAliyunDetailMemoryAPI(WebTokenAuthentication, APIView):
Expand All @@ -69,8 +69,7 @@ def get(self, request, *args, **kwargs):
time_pick(API, time)
API.request_to_instance(API,self.get_object().aliyun_id)
API.get_mem_results()
results = API.get_line_opts(API.get_results(), '内存使用率')
return Response(results, status.HTTP_200_OK)
return Response({'title':'内存使用率', 'dataset': API.change_timestamp(API.get_results())})


class MonitorHostAliyunDetailIReadIOPS(WebTokenAuthentication, APIView):
Expand All @@ -86,8 +85,7 @@ def get(self, request, *args, **kwargs):
time_pick(API, time)
API.request_to_instance(API, self.get_object().aliyun_id)
API.get_read_iops_results()
results = API.get_line_opts(API.get_results(), '磁盘读取Count/Second')
return Response(results, status.HTTP_200_OK)
return Response({'title': '磁盘读取Count/Second', 'dataset': API.change_timestamp(API.get_results())})


class MonitorHostAliyunDetailInternetInRate(WebTokenAuthentication, APIView):
Expand All @@ -103,5 +101,4 @@ def get(self, request, *args, **kwargs):
time_pick(API, time)
API.request_to_instance(API, self.get_object().aliyun_id)
API.get_in_net_results()
results = API.get_line_opts(API.get_results(), '网络流入流量bits/s')
return Response(results, status.HTTP_200_OK)
return Response({'title': '网络流入流量bits/s', 'dataset': API.change_timestamp(API.get_results())})
98 changes: 49 additions & 49 deletions apps/zdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,51 @@
"User"
]

class InstanceGroup(models.Model):
id = models.AutoField(primary_key=True)
uuid = models.UUIDField(auto_created=True, default=uuid.uuid4, editable=False)

name = models.CharField(max_length=200, default='')
group = models.OneToOneField(Group, related_name='dbgroup', on_delete=models.SET_NULL, null=True, blank=True)

class Meta:
permissions = (
('yo_list_db_group', u'罗列数据库实例组'),
('yo_create_db_group', u'新增数据库实例组'),
('yo_update_db_group', u'修改数据库实例组'),
('yo_delete_db_group', u'删除数据库实例组'),
)

@property
def status(self):
status = settings.STATUS_DB_INSTANCE_CAN_BE_USE
if self.instances.count() != 0:
for instance in self.instances.all():
status = instance.status and status
return status
else:
return settings.STATUS_DB_INSTANCE_UNREACHABLE


@property
def group_name(self):
if self.group is not None:
return self.group.name
else:
return ""

@property
def instance_count(self):
return self.instances.count()

@property
def database_count(self):
count = 0
for instance in self.instances.all():
count = count + instance.databases.count()
return count


class Instance(models.Model):
INSTANCE_STATUS = (
(settings.STATUS_DB_INSTANCE_PASSWORD_WRONG, '密码错误'),
Expand All @@ -33,6 +78,8 @@ class Instance(models.Model):
_connect_ip = models.CharField(max_length=300, default='', blank=True, null=True)
port = models.IntegerField(default=3306)

group = models.ForeignKey(InstanceGroup, related_name="instances", on_delete=models.SET_NULL, null=True, blank=True)

# 阿里云RDS
aliyun_id = models.CharField(max_length=30, default='', blank=True, null=True)

Expand Down Expand Up @@ -80,14 +127,14 @@ def connect_ip(self,connect_ip):

@property
def group_name(self):
return self.group.get().name
return self.group.name

@property
def password(self):
if self._admin_passwd:
return aes.decrypt(self._admin_passwd)
else:
return 'nopassword'
return ''

@password.setter
def password(self, password):
Expand Down Expand Up @@ -121,50 +168,3 @@ def password(self):
def password(self, password):
self._passwd = aes.encrypt(password).decode()



class InstanceGroup(models.Model):
id = models.AutoField(primary_key=True)
uuid = models.UUIDField(auto_created=True, default=uuid.uuid4, editable=False)

name = models.CharField(max_length=200, default='')
group = models.OneToOneField(Group, related_name='dbgroup', on_delete=models.SET_NULL, null=True, blank=True)
instances = models.ManyToManyField(Instance, blank=True, related_name='group', verbose_name=_("instances"))

class Meta:
permissions = (
('yo_list_db_group', u'罗列数据库实例组'),
('yo_create_db_group', u'新增数据库实例组'),
('yo_update_db_group', u'修改数据库实例组'),
('yo_delete_db_group', u'删除数据库实例组'),
)

@property
def status(self):
status = settings.STATUS_DB_INSTANCE_CAN_BE_USE
if self.instances.count() != 0:
for instance in self.instances.all():
status = instance.status and status
return status
else:
return settings.STATUS_DB_INSTANCE_UNREACHABLE


@property
def group_name(self):
if self.group is not None:
return self.group.name
else:
return ""

@property
def instance_count(self):
return self.instances.count()

@property
def database_count(self):
count = 0
for instance in self.instances.all():
count = count + instance.databases.count()
return count

18 changes: 12 additions & 6 deletions apps/zdb/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from zdb import models
from manager.models import Group,Host
from zdb.tasks import instance_create
from zdb.tasks import status_flush
__all__ = [
'ZDBInstanceGroupSerializer', "ZDBInstanceCreateSerializer",
"ZDBInstanceImportSerializer", "ZDBInstanceSerializer",
Expand Down Expand Up @@ -31,35 +32,40 @@ class Meta:
class ZDBInstanceSerializer(serializers.HyperlinkedModelSerializer):
host = serializers.PrimaryKeyRelatedField(required=False, allow_null=True,
queryset=models.Host.objects.all())
_status = serializers.IntegerField(required=False, source='status', )
passwd = serializers.CharField(required=False, allow_null=True, source='password', write_only=True)

_connect_ip = serializers.CharField(required=False, allow_null=True, source='connect_ip', )
group = serializers.PrimaryKeyRelatedField(required=True, allow_null=False, queryset=models.InstanceGroup.objects.all())

groupname = serializers.CharField(source="group_name", read_only=True)

class Meta:
model = models.Instance
fields = (
'id', 'uuid', 'name', '_connect_ip', 'port', 'aliyun_id', 'host', 'admin_user', '_status', 'passwd', 'type',
'id', 'uuid', 'name', '_connect_ip', 'port', 'aliyun_id', 'host', 'admin_user', 'status', 'passwd', 'type', 'group',
'groupname'
)
read_only_fields = (
'id', 'uuid', 'groupname'
'id', 'uuid', 'groupname', 'status'
)


class ZDBInstanceImportSerializer(ZDBInstanceSerializer):
class Meta:
model = models.Instance
fields = (
'id', 'uuid', 'name', '_connect_ip', 'port', 'aliyun_id', 'host', 'admin_user', '_status', 'passwd', 'type', 'groupname'
'id', 'uuid', 'name', '_connect_ip', 'port', 'aliyun_id', 'host', 'admin_user', 'status', 'passwd', 'type', 'groupname',
'group'
)
read_only_fields = (
'id', 'uuid', 'groupname'
'id', 'uuid', 'groupname', 'status'
)

def create(self, validated_data):
return super(ZDBInstanceImportSerializer, self).create(validated_data)
obj = super(ZDBInstanceImportSerializer, self).create(validated_data)
status_flush.delay(obj)
return obj


class DetailSerializer(serializers.Serializer):
memory = serializers.IntegerField()
Expand Down
21 changes: 14 additions & 7 deletions apps/zdb/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ def status_flush(instance):
instance.save()
return

# try:
# db = MySQLdb.connect(host=instance.connect_ip,port=instance.port,user=instance.admin_user,passwd=instance.password)
# except MySQLdb.connections.OperationalError as e:
# instance._status = settings.STATUS_DB_INSTANCE_PASSWORD_WRONG
# instance.save()
# return
#
try:
print(instance.connect_ip, instance.port, instance.admin_user, instance.password)
db = MySQLdb.connect(
host=instance.connect_ip,
port=instance.port,
user=instance.admin_user,
password=instance.password
)
except MySQLdb.connections.OperationalError as e:
print('password wrong',e)
instance._status = settings.STATUS_DB_INSTANCE_PASSWORD_WRONG
instance.save()
return

instance._status = settings.STATUS_DB_INSTANCE_CAN_BE_USE
instance.save()

Expand Down
1 change: 0 additions & 1 deletion deveops/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@
AUTH_LDAP_USER_SEARCH = LDAPSearch(OU,ldap.SCOPE_SUBTREE,"(&(objectClass=*)(sAMAccountName=%(user)s))")
AUTH_LDAP_USER_ATTR_MAP = {
"full_name": "cn",
"description": "description",
"first_name":"sn",
"phone":"mobile",
"groups": "",
Expand Down
17 changes: 15 additions & 2 deletions deveops/tools/aliyun/cms.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ def get_mem_results(self):
def get_load_results(self):
pass

@staticmethod
def change_timestamp(dataset):
json_dataset = []
for data in json.loads(dataset):
d = datetime.datetime.fromtimestamp(data['timestamp'] / 1000)
str1 = d.strftime("%Y/%m/%d %H:%M:%S") # "%Y/%m/%d %H:%M:%S"
data['timestamp'] = str1
json_dataset.append(data)
return json_dataset


# :TODO Removed Function
@staticmethod
def get_line_opts(results, title):
import datetime
Expand All @@ -96,11 +108,12 @@ def get_line_opts(results, title):
if not results:
return {}
for result in json.loads(results):
d = datetime.datetime.fromtimestamp(result['timestamp']/1000)
str1 = d.strftime("%Y/%m/%d %H:%M:%S") #"%Y/%m/%d %H:%M:%S"
d = datetime.datetime.fromtimestamp(result['timestamp'] / 1000)
str1 = d.strftime("%Y/%m/%d %H:%M:%S") # "%Y/%m/%d %H:%M:%S"
time.append(str1)
minimum.append(round(result['Minimum'],2))
maximum.append(round(result['Maximum'],2))

# average.append(result['Average'])
from pyecharts import Line
from pyecharts.base import TRANSLATOR
Expand Down

0 comments on commit 7f33106

Please sign in to comment.