授之以渔-运维平台应用模块三(BIND篇)

###暴雪爸爸开了8.0,我去保卫艾泽拉斯了,很久没有更新。公司用的BIND做的DNS管理,采用了文本格式存储。之前用过BIND+MYSQL+DLZ,对于后面想要自行搭建运维可视化平台的小伙伴来说,确实很方便。但性能问题是个大问题,最后无奈采用通过Dnspython封装的BIND接口,操作BIND的jnl文件进行动态解析。
附上一个dlz和原生bind的性能测试数据(比较老,仅供参考):
image.png

效果图如下:
image.png
image.png
image.png

#一、 Jnl文件
jnl 文件(journal文件)是bind动态更新的时候记录更新内容所生成的日志文件。BIND9支持两个授予用户对一个域执行动态更新权限的备选方案,分别由allow-update和update-policy设定。其中allow-update 授于指定用户对域中的任何名称的任何的记录更新的权利。allow-update指定哪些主机允许为主域名服务器提交动态 DNS 更新。默认为拒绝任何主机进行更新,详见百度。

##二、 环境准备

  • Dnspython对应你的python版本,官网http://www.dnspython.org/
  • Django运行版本1.6.1
  • Python运行版本2.7.1

##三、 食用过程
###1.生成TSIG key,修改named.conf添加授权,定义zone文件添加allow-update(详见百度,这里不具体展开)
###2.增删改后端部分代码

# -*- coding: utf8 -*-
import dns.tsigkeyring
import dns.update
import dns.query 
import dns.resolver
keyring = dns.tsigkeyring.from_text({"youth":tsig key密码})
up = dns.update.Update("youth.cn", keyring=keyring)
up .add("test2", 60, 'a', "192.168.183.131")    #添加名称为test2的A记录,TTL时间60,解析地址192.168.183.131
up.delete("test2")  #删除test2记录
up.replace("test2", 360, "CNAME", "test3.youth.cn.")  #将test2修改为CNAME记录,TTL时间360,CNAME地址test3.youth.cn.
dns.query.tcp(up, SERVER_IP)

###3.前端用的Metronic框架,jQuery表格插件用的dataTables,部分前端代码如下:

<div class="page-bar">
	<ul class="page-breadcrumb">
		<li>
			<i class="fa fa-home"></i>
			<a href="../dashboard/">主页</a>
			<i class="fa fa-angle-right"></i>			
		</li>	
		<li>
			<a href="../bind_list/">DNS管理系统</a>
			<i class="fa fa-angle-right"></i>
		</li>
		<li>
			<a href="../bind_list/" id="history_url">DNS管理系统汇总</a>
		</li>
	</ul>
</div>

<div class="portlet">
	<form class="tel_search-form" method="get" action="">
		<div class="row">
			<div class="col-md-12">
				<div class="input-group input-xlarge">
                    {%  for i in  user.groups.values %}{% ifequal i.name 'admin' %}
					<div class="input-group-btn">
						<select	class="form-control input-small" name="bind_zone">
                            <option value=""> 请选择记录域 </option>
                            <option value="2"> youth.cn </option>
                            <option value="1"> k618.cn </option>
                            <option value="3"> ccyl.org.cn </option>
                            <option value="4"> gqt.org.cn </option>
						</select>
					</div>
                    {% else %}{% endifequal %}{% endfor %}
					<div class="input-group-btn">
						<select	class="form-control input-small" name="bind_type">
                            <option value=""> 请选择记录类型 </option>
                            <option value="1"> A记录 </option>
                            <option value="2"> AAAA记录 </option>
                            <option value="6"> CNAME记录 </option>
                            <option value="3"> MX记录 </option>
                            <option value="5"> TXT记录 </option>
                            <option value="4"> NS记录 </option>
						</select>
					</div>
					<div class="input-group-btn">
						<select	class="form-control input-medium" name="bind_internal_external">
                            <option value=""> 请选择内外部解析类型 </option>
                            <option value="1"> 内部解析 </option>
                            <option value="2"> 外部解析</option>
						</select>
					</div>
					<input type="text" name="search" placeholder="请输入查询名或者服务器IP " class="form-control input-large">
					<span class="input-group-btn">	
						<button data-loading-text="等待提交..." class="loading-btn btn green" type="submit">查找 <i class="fa fa-search"></i></button>
						<a class="btn red" href="#responsive" margin-bottomrole="button" data-toggle="modal"><span > 添加DNS解析记录<i class="fa fa-plus"> </i></span></a>
					</span>					
				</div>
			</div>	
		</div>
	</form>	
</div>

<div class="portlet box green">
	<div class="portlet-title">
		<div class="caption"> <i class="icon-globe"> </i> DNS解析记录汇总 </div>
		<div class="tools"> <a href="javascript:;" class="collapse"> </a> <a href="javascript:;" class="reload"> </a> <a href="javascript:;" class="remove"> </a> </div>
	</div>
	<div class="portlet-body">
		<table class="table table-striped table-bordered table-condensed" id="bindlist">
            <thead>
                <tr>
                    <th><b> ID </b> </th>
                    <th><b> 名称 </b> </th>
                    <th><b> 域 </b> </th>
                    <th><b> 类型 </b> </th>
                    <th><b> 内部/外部 </b> </th>
                    <th><b> 解析地址 </b> </th>
                    <th><b> TTL </b> </th>
                    <th><b> 备注解析地址 </b> </th>
                    <th><b> 更改时间 </b> </th>
                    <th><b> 查看状态 </b> </th>
                    <th><b> 更改操作 </b> </th>
                    <th><b> 删除操作 </b> </th>
                </tr>
            </thead>
            <tbody>
                {% for bind in binds%}
                <tr>
                    <td id={{ bind.id }}> {{ bind.id }} </td>
                    <td id={{ bind.bind_name }}> {{ bind.bind_name }} </td>
                    <td id={{bind.bind_zone_id}}>
                       {% ifequal  bind.bind_zone_id '1' %}
                        <span class="label label-sm label-success">
                        k618.cn
                        </span>
                        {% endifequal %}
                       {% ifequal  bind.bind_zone_id '2' %}
                        <span class="label label-sm label-danger">
                        youth.cn
                        </span>
                        {% endifequal %}
                       {% ifequal  bind.bind_zone_id '3' %}
                        <span class="label label-sm label-warning">
                        ccyl.org.cn
                        </span>
                        {% endifequal %}
                       {% ifequal  bind.bind_zone_id '4' %}
                        <span class="label label-sm label-info">
                        gqt.org.cn
                        </span>
                        {% endifequal %}
                    </td>
                    <td id={{ bind.bind_type_id}}>
                       {% ifequal  bind.bind_type_id '1' %}
                        <span class="label label-sm label-warning">
                        A记录
                        </span>
                        {% endifequal %}
                       {% ifequal  bind.bind_type_id '6' %}
                        <span class="label label-sm label-info">
                        CNAME记录
                        </span>
                        {% endifequal %}
                       {% ifequal  bind.bind_type_id '3' %}
                        <span class="label label-sm label-success">
                        MX记录
                        </span>
                        {% endifequal %}
                       {% ifequal  bind.bind_type_id '4' %}
                        <span class="label label-sm label-danger">
                        NS记录
                        </span>
                       {% endifequal %}
                       {% ifequal  bind.bind_type_id '2' %}
                        <span class="label label-sm label-danger">
                        AAAA记录
                        </span>
                       {% endifequal %}
                       {% ifequal  bind.bind_type_id '5' %}
                        <span class="label label-sm label-danger">
                        TXT记录
                        </span>
                        {% endifequal %}
                    </td>
                    <td id={{bind.bind_internal_external}}>
                       {% ifequal  bind.bind_internal_external '1' %}
                        <span class="label label-sm label-success">
                        内部解析
                        </span>
                        {% endifequal %}
                       {% ifequal  bind.bind_internal_external '2' %}
                        <span class="label label-sm label-warning">
                        外部解析
                        </span>
                        {% endifequal %}
                    </td>
                    <td id={{ bind.bind_ip}}> {{ bind.bind_ip}} </td>
                    <td id={{ bind.bind_ttl}}> {{ bind.bind_ttl}} </td>
                    <td id={{ bind.bind_intro}}> {{ bind.bind_intro}} </td>
                    <td> {{ bind.bind_time|date:"Y-m-d H:i:s"}} </td>
                    <td><a class="fa fa-eye" href="../bind_see/?bind_name={{bind.bind_name}}&bind_zone={{bind.bind_zone}}&bind_ip={{bind.bind_ip}}"></a></td>
                    <td><a class="fa fa-edit" href="#responsives2" data-toggle="modal"></a></td>
                    <td><a class="fa fa-trash-o" href="#responsivess" data-toggle="modal"></a></td>
                {% endfor %}
                </tr>
            </tbody>
		</table>
	</div>
</div>

<div class="modal fade" id="responsive" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button>
				<h3 id="myModalLabel"> 添加DNS解析记录 </h3>
			</div>
			<form action="" method="post" class="form-horizontal">
					{% csrf_token %}
				<div class="modal-body">
					<div class="form-group">
						<label class="control-label col-md-3"> 名称 </label>
						<div class="col-md-5">
							<input type="text" class="form-control" placeholder="例如:cms" name="bind_name">
						</div>
					</div>
					<div class="form-group" >
						<label class="control-label col-md-3"> TTL </label>
						<div class="col-md-5">
							<input type="text" class="form-control" placeholder="例如:3600" name="bind_ttl">
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3"> Zone域 </label>
						<div class="col-md-5">
							<select	class="form-control input-medium select2me select2-offscreen" name="bind_zone">
								<option value="2"> youth.cn </option>
								<option value="1"> k618.cn </option>
								<option value="3"> ccyl.org.cn </option>
								<option value="4"> gqt.org.cn </option>												
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3"> 类型 </label>
						<div class="col-md-5">
							<select	class="form-control input-medium select2me select2-offscreen" name="bind_type"	onchange="add_bind_intro_hidden(this)">
								<option value="1"> A记录 </option>
								<option value="2"> AAAA记录 </option>
								<option value="6"> CNAME记录 </option>
								<option value="3"> MX记录 </option>
								<option value="5"> TXT记录 </option>
								<option value="4"> NS记录 </option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3"> 解析地址 </label>
						<div class="col-md-5">
							<input type="text" class="form-control"	placeholder="例如:123.103.56.246" name="bind_ip">
						</div>
					</div>
					<div class="form-group" id="add_bind_intro" style="display:none;">
						<label class="control-label col-md-3"> 备注解析地址 </label>
						<div class="col-md-5">
							<input type="text" class="form-control"	placeholder="例如:123.103.56.246" name="bind_intro">
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3"> 解析类型 </label>
						<div class="col-md-5">
							<select	class="form-control input-medium select2me select2-offscreen" name="bind_internal_external"	onchange="edit_bind_internal_external_hidden(this)">
								<option value="1"> 内部解析 </option>
								<option value="2"> 外部解析 </option>
							</select>
						</div>
					</div>
					<div class="form-group" id="add_bind_url_mitors" style="display:none;">
						<label class="control-label col-md-3">URL监控</label>
						<div class="col-md-5">
							<div class="basic-toggle-button" >
								<input type="checkbox" class="make-switch" id="add_url_mitor" checked="checked" data-size="small"/>
								<input type="hidden" name="add_url_mitors" value="true" id="add_url_mitors"/>
							</div>
						</div>
					</div>
					<div class="form-group" id="add_bind_syncs" style="display:none;">
						<label class="control-label col-md-3">同步网宿节点</label>
						<div class="col-md-5">
							<div class="basic-toggle-button" >
								<input type="checkbox" class="make-switch" id="add_sync" checked="checked" data-size="small"/>
								<input type="hidden" name="add_syncs" value="true" id="add_syncs"/>
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
                    <button data-loading-text="等待提交..." class="loading-btn btn green" type="submit" name="submit"> 确认执行 </button>
                    <button class="btn red" data-dismiss="modal" aria-hidden="true"> 取消执行 </button>
				</div>
			</form>
		</div>	
	</div>		
</div>

<div class="modal fade" id="responsivess" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button>
				<input type="hidden" id="del_name">
			</div>
			<form action="" method="get" class="form-horizontal">
					{% csrf_token %}
                <div class="modal-body">
                    <div class="form-group">
                        <div class="col-md-5">
                            <span class="label label-danger">注意:删除解析记录,相应的监控记录也会一并删除!!</span>
                        </div>
                    </div>
                </div>
				<div class="modal-footer">
					<label class="control-label"> </label>
					<div class="controls">
                        <button data-loading-text="等待提交..." class="loading-btn btn green" type="submit" name="submit"> 确认执行 </button>
                        <button class="btn red" data-dismiss="modal" aria-hidden="true"> 取消执行 </button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>

<div class="modal fade" id="responsives2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button>
				<h3 id="myModalLabel"> 修改DNS解析记录 </h3>
			</div>
			<form action="" method="post" class="form-horizontal">
					{% csrf_token %}
				<div class="modal-body">
					<div class="form-group" type="hidden">
						<div class="col-md-5">
							<input type="text" id="edit_bind_id">
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3"> 名称 </label>
						<div class="col-md-5">
							<input type="text" id="edit_bind_name">
						</div>
					</div>
					<div class="form-group" >
						<label class="control-label col-md-3"> TTL </label>
						<div class="col-md-5">
							<input type="text" id="edit_bind_ttl">
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3"> Zone域 </label>
						<div class="col-md-5">
							<select	class="form-control input-medium select2me select2-offscreen" name="bind_zone" id="edit_bind_zone">
								<option value="2"> youth.cn </option>
								<option value="1"> k618.cn </option>
								<option value="3"> ccyl.org.cn </option>
								<option value="4"> gqt.org.cn </option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3"> 类型 </label>
						<div class="col-md-5">
							<select	class="form-control input-medium select2me select2-offscreen" name="bind_type"	id="edit_bind_type" onchange="edit_bind_intro_hidden(this)">
								<option value="1"> A记录 </option>
								<option value="2"> AAAA记录 </option>
								<option value="6"> CNAME记录 </option>
								<option value="3"> MX记录 </option>
								<option value="5"> TXT记录 </option>
								<option value="4"> NS记录 </option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3"> 解析地址 </label>
						<div class="col-md-5">
							<input type="text" id="edit_bind_ip">
						</div>
					</div>
					<div class="form-group" id="edit_bind_intro" style="display:none;">
						<label class="control-label col-md-3"> 备注解析地址 </label>
						<div class="col-md-5">
                            <input type="text" id="edit_bind_intros">
						</div>
					</div>
				</div>
				<div class="modal-footer">
                    <button data-loading-text="等待提交..." class="loading-btn btn green" type="submit" name="submit"> 确认执行 </button>
                    <button class="btn red" data-dismiss="modal" aria-hidden="true"> 取消执行 </button>
				</div>
			</form>
		</div>
	</div>
</div>

这块做了一个异步的查询,其实就是点击+号,能返回到这个域名绑定的公网IP所对应的主机,效果图如下:
image.png

var TableAdvanced = function () {

    var initTable4 = function () {
        var table = $('#bindlist');

        var nCloneTh = document.createElement('th');
        nCloneTh.className = "table-checkbox";

        var nCloneTd = document.createElement('td');
        nCloneTd.innerHTML = '<span class="row-details row-details-close"></span>';

        table.find('thead tr').each(function () {
            this.insertBefore(nCloneTh, this.childNodes[0]);
        });

        table.find('tbody tr').each(function () {
            this.insertBefore(nCloneTd.cloneNode(true), this.childNodes[0]);
        });

        var oTable = table.dataTable({

            // Internationalisation. For more info refer to http://datatables.net/manual/i18n
            "language": {
                "aria": {
                    "sortAscending": ": activate to sort column ascending",
                    "sortDescending": ": activate to sort column descending"
                },
                "emptyTable": "未有相关数据",
                "info": "当前显示 _START_ 到 _END_ 条,共 _TOTAL_ 条记录。",
                "infoEmpty": "当前显示0到0条,共0条记录",
                "infoFiltered": "(数据库中共为 _MAX_ 条记录)",
                "lengthMenu": "显示 _MENU_ 记录",
                "search": "模糊查询:",
                "zeroRecords": "对不起,查询不到任何相关数据",
               "oPaginate": {
                   "sFirst": "首页",
                   "sPrevious": " 上一页 ",
                   "sNext": " 下一页 ",
                   "sLast": " 尾页 "
               }				
            },

            "columnDefs": [{
                "orderable": false,
                "targets": [0]
            }],
            "order": [
                [9, 'dec']
            ],
            "lengthMenu": [
                [5, 15, 20, -1],
                [5, 15, 20, "All"] // change per page values here
            ],
            // set the initial value
            "pageLength": 10
        });

        var tableWrapper = $('#sample_4_wrapper'); // datatable creates the table wrapper by adding with id {your_table_jd}_wrapper
        var tableColumnToggler = $('#sample_4_column_toggler');

        /* modify datatable control inputs */
        tableWrapper.find('.dataTables_length select').select2(); // initialize select2 dropdown
        table.on('click', ' tbody td .row-details', function () {
            var nTr = $(this).parents('tr')[0];
            if (oTable.fnIsOpen(nTr)) {
                $(this).addClass("row-details-close").removeClass("row-details-open");
                oTable.fnClose(nTr);
            } else {
                var ip = $(this).parent().parent().find('td').eq(6).attr("id");
                var type = $(this).parent().parent().find('td').eq(4).attr("id");
                var intro = $(this).parent().parent().find('td').eq(8).attr("id");
                $(this).addClass("row-details-open").removeClass("row-details-close");;
                fnFormatDetails(nTr, $(this).attr("data_id"),ip,type,intro);
            }
        });
          function fnFormatDetails(nTr, pdataId, ip ,type ,intro) {
                var aData = oTable.fnGetData(nTr);
               //根据配置Id 异步查询数据
                $.ajax({
                       type:'get',
                       url:"../bind_host_get/?ip="+ip+"&type="+type+"&intro="+intro,
                       data:{"pdataId":pdataId},
                       dataType:"json",
                       async:true,
                       beforeSend:function(xhr){//信息加载中
                           oTable.fnOpen( nTr, '<span id="configure_chart_loading"><img src="../assets/global/img/input-spinner.gif"/> 详细信息加载中...</span>', 'details' );
                       },
                       success:function (obj){
                               var sOut = '<table class="table table-striped table-bordered table-hover">';
                               for(var id in obj){
                                    sOut += '<tr><td>'+id+'</td><td>' + obj[id] + '</td></tr>';
                               }
                               sOut+='</table>';
                               oTable.fnOpen( nTr,sOut, 'details' );

                       },
                       error: function(){//请求出错处理
                           oTable.fnOpen( nTr,'加载数据超时', 'details' );
                       }
                   });

           }

        /* handle show/hide columns*/
        $('input[type="checkbox"]', tableColumnToggler).change(function () {
            /* Get the DataTables object again - this is not a recreation, just a get of the object */
            var iCol = parseInt($(this).attr("data-column"));
            var bVis = oTable.fnSettings().aoColumns[iCol].bVisible;
            oTable.fnSetColumnVis(iCol, (bVis ? false : true));
        });
    }

    return {

        //main function to initiate the module
        init: function () {

            if (!jQuery().dataTable) {
                return;
            }

            console.log('me 1');


            initTable4();


            console.log('me 2');
        }

    };

}();

####这里踩过的坑:
最终公司的DNS惨遭了30G的DD,导致运营商都跟着一起倒霉,后续将NS解析迁移至网宿的云DNS平台,我的平台还是通过网宿的云DNS平台所提供的接口API进行域名解析,BIND只作为内网解析及解析记录汇总(即选择外部解析后,解析记录将存储在我的内部平台上并推送记录到网宿节点,这也是为了将来万一换厂家,直接推送全部记录即可)
image.png
####URL监控用的Openfalcon做的,后续会慢慢补上。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值