2018-02-20 11:57:39   Visit  17

psutil是python的一个扩展库,能够轻松获取服务器状态信息,用于系统监控。

#encoding:utf-8
__author__ = \\\'leechg\\\'

import psutil
import datetime

#内存
def getMem():
    mem = psutil.virtual_memory()
    return mem
#内存总量
def getMemTotal():
    mem = getMem()
    total = mem.total
    return total
#已用内存数量
def getMemUsed():
    mem = getMem()
    used = mem.used
    return used
#空闲内存
def getMemFree():
    mem = getMem()
    return mem.free
#内存占用率
def getMemUsedPercent():
    return int(getMemUsed()/float(getMemTotal())*100)
#逻辑处理器数量
def getCpuCount():
    return psutil.cpu_count()
#物理处理器数量
def getLogicalCpuCount():
    return psutil.cpu_count()
#cpu时间
def getCpuTime():
    return psutil.cpu_times().user
#磁盘信息
def getDisk():
    return psutil.disk_partitions()
#磁盘使用状态
def getDiskUsage(device = \\\"C:\\\\\\\\\\\"):
    return  psutil.disk_usage(device)
#网络状态
def getNetState():
    return psutil.net_io_counters(pernic=True)
#用户信息
def getUserInfo():
    return psutil.users()
#系统启动时间
def getBoottime():
    return psutil.boot_time()
#时间戳转为自然时间
def timestapToLocalStr(tt):
    return  datetime.datetime.fromtimestamp(tt).strftime(\\\"%Y-%m-%d %H:%M:%S\\\")


if __name__==\\\"__main__\\\":
    print \\\"Leechg\\\"
    print getMem()
    print getMemTotal()/1024/1024,\\\"M\\\"
    print getMemUsed()/1024/1024,\\\"M\\\"
    print getMemFree()/1024/1024,\\\"M\\\"
    print getMemUsedPercent()
    print getCpuCount()
    print getLogicalCpuCount()
    print getCpuTime()
    print getDisk()
    print getDiskUsage(\\\"d:\\\\\\\\\\\")
    print getNetState()
    print getUserInfo()
    print timestapToLocalStr(getBoottime())
Leechg
svmem(total=8504565760L, available=3029106688L, percent=64.4, used=5475459072L, free=3029106688L)
8110 M
5221 M
2888 M
64
8
8
587414.125
[sdiskpart(device=\'C:\\\\\', mountpoint=\'C:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'D:\\\\\', mountpoint=\'D:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'E:\\\\\', mountpoint=\'E:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'F:\\\\\', mountpoint=\'F:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'G:\\\\\', mountpoint=\'G:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'H:\\\\\', mountpoint=\'H:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'I:\\\\\', mountpoint=\'I:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'K:\\\\\', mountpoint=\'K:\\\\\', fstype=\'NTFS\', opts=\'rw,fixed\'), sdiskpart(device=\'M:\\\\\', mountpoint=\'M:\\\\\', fstype=\'\', opts=\'cdrom\')]
sdiskusage(total=107374178304L, used=34099097600L, free=73275080704L, percent=31.8)
{\'\\xd2\\xd4\\xcc\\xab\\xcd\\xf8 2\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'\\xd2\\xd4\\xcc\\xab\\xcd\\xf8\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'\\xc0\\xb6\\xd1\\xc0\\xcd\\xf8\\xc2\\xe7\\xc1\\xac\\xbd\\xd3\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'Loopback Pseudo-Interface 1\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'WLAN\': snetio(bytes_sent=10181550L, bytes_recv=106462649L, packets_sent=62924L, packets_recv=253769L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'\\xb1\\xbe\\xb5\\xd8\\xc1\\xac\\xbd\\xd3* 3\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), \'\\xb1\\xbe\\xb5\\xd8\\xc1\\xac\\xbd\\xd3* 1\': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L)}
[suser(name=\'licha\', terminal=None, host=\'0.0.0.0\', started=1518789595.0, pid=None)]
2018-02-03 09:25:00
2017-12-23 12:36:54   Visit  69

Chrome测试获取地理坐标被禁止

Chrome测试获取地理坐标被禁止,控制台提示以下信息

[Deprecation] getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://goo.gl/rStTGz for more details.

在测试环境下只需要修改Chrome浏览器的安全策略就可以,http://www.domian.com 换成自己的域名

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --unsafely-treat-insecure-origin-as-secure="http://www.domian.com" flag --user-data-dir=C:\testprofile
2017-12-08 22:58:09   Visit  77

MSwipe

属性

属性名称属性类型属性说明默认值
moveWithTouchboolean是否触摸滑动false
moveDirectionstring滑动方向,moveWithTouch为true时生效down
maxMovenumber最大移动距离0
moveTimenumber超时时间0
tolerancenumber误差25
moveBackboolean是否回弹true
isStopPropagationboolean是否取消冒泡false
lisobjectlis
eleobjectele
movedbooleanmovedfalse
startTouchobjectstartTouch
listenerNodeobject组件监听托管元素

方法

option | option

asMSwipe(option)

  • 重新设置参数

参数

参数名称 | 参数说明

optionoption

disable()

  • 设置不可编辑

参数

参数名称 | 参数说明

enable()

  • 设置可编辑

参数

参数名称 | 参数说明

readOnly()

  • 设置非只读

参数

参数名称 | 参数说明

editable()

参数

参数名称参数说明

hide()

  • 隐藏元素

参数

参数名称 | 参数说明

show()

  • 显示元素

参数

参数名称 | 参数说明

2017-12-08 15:39:51   Visit  89

JavaScript 生成 SVG 圆角矩形 PATH

  var length = 12 * text.length; //横向直线长度
     
          var height = 28;//高度
          var r = 10;//圆角半径
          var half_h = height/2;
          var s_half_h = half_h - r;
          var H_all = length + r + r;//横向总长度
          var path = "M0 0 V " + s_half_h + " "; 
          path += "Q 0 "+ half_h+" "+ r +" "+half_h+" H " + (length  + r) + " ";
          path += "Q " + H_all + " " + half_h + " " + H_all + " " + s_half_h +" V -" + s_half_h + " ";
          path += "Q " + H_all + " -" + half_h + " " + (H_all-r) + " -" + half_h +" H " + r + " "; 
          path += "Q 0 -" + half_h + " 0 -" + s_half_h +" V " + 0 + " Z"; 
2017-11-08 16:52:19   Visit  109

Tomcat、memcached、Nginx环境下共享session

Tomcat 配置

<Manager       
        className="de.javakaffee.web.msm.MemcachedBackupSessionManager" 
        memcachedNodes="n1:127.0.0.1:11211" 
        sticky="false" 
        sessionBackupAsync="false" 
        copyCollectionsForSerialization="false" 
        
        transcoderFactoryClass="de.javakaffee.web.msm.JavaSerializationTranscoderFactory" 
      />

memcachedNodes 多个节点使用英文逗号隔开。

memcached相关jar


jar放在Tomcat lib目录下。

2017-11-03 18:16:11   Visit  104
        location /  {
            proxy_pass http://tomcat_server; 
            proxy_set_header Host $http_host; 
            proxy_set_header X-Forward-For $remote_addr; 
            proxy_connect_timeout       10;
            proxy_read_timeout          10;
            proxy_send_timeout          10;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
2017-10-30 15:44:36   Visit  118

在java中如何对一组数据做排序?

Java原生提供了这一支持,所以我们没必要在开发中再造轮子。这里我们使用的是Comparable接口。
要对比的对象需要实现Comparable接口,并实现一个方法:

compareTo(<T>)

该方法返回1或者-1(最好是互为反数)。

排序

Collections.sort(list);  

下面是我的代码

@Entity
@Table(name = "t_s_dddepart", schema = "")
@SuppressWarnings("serial")
public class DingDingDepart     implements java.io.Serializable , Comparable<DingDingDepart> {
    /**id*/
    private java.lang.Integer id;
    /**name*/
    @Excel(name="name")
    private java.lang.String name;
    /**parentid*/
    @Excel(name="parentid")
    private java.lang.Integer parentid;
    
    /**
     *方法: 取得java.lang.Integer
     *@return: java.lang.Integer  id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name ="ID",nullable=false,length=13)
    public java.lang.Integer getId(){
        return this.id;
    }

    /**
     *方法: 设置java.lang.Integer
     *@param: java.lang.Integer  id
     */
    public void setId(java.lang.Integer id){
        this.id = id;
    }
    /**
     *方法: 取得java.lang.String
     *@return: java.lang.String  name
     */
    @Column(name ="NAME",nullable=true,length=255)
    public java.lang.String getName(){
        return this.name;
    }

    /**
     *方法: 设置java.lang.String
     *@param: java.lang.String  name
     */
    public void setName(java.lang.String name){
        this.name = name;
    }
    /**
     *方法: 取得java.lang.Integer
     *@return: java.lang.Integer  parentid
     */
    @Column(name ="PARENTID",nullable=true,length=13)
    public java.lang.Integer getParentid(){
        return this.parentid;
    }

    /**
     *方法: 设置java.lang.Integer
     *@param: java.lang.Integer  parentid
     */
    public void setParentid(java.lang.Integer parentid){
        this.parentid = parentid;
    }

    @Override
    public int compareTo(DingDingDepart o2) {
         if( this.getId() == this.getParentid())  
             return 1;  
         //注意!!返回值必须是一对相反数,否则无效。jdk1.7以后就是这样。  
         //else return 0; 
         //无效  
         else return -1;  
    }
    
    public static void main(String[] args) throws Exception{
        System.out.println("11111");
        DingDingUtil.getAccess_Token();
         
        List<DingDingDepart> list = new ArrayList<DingDingDepart>(); 
        JSONArray departArr = DingDingUtil.getDingDepart();
        for (int i = 0; i < departArr.size(); i++) {
            DingDingDepart depart = new DingDingDepart();
            JSONObject jsonItem = departArr.getJSONObject(i);
            depart.setId(jsonItem.getInteger("id"));
            depart.setName(jsonItem.getString("name"));
            Object pid = jsonItem.get("parentid");
            if(pid!=null){
                depart.setParentid(jsonItem.getInteger("parentid"));
            }else{
                depart.setParentid(0);
            }
            
             
            list.add(depart);
        }
        String a= "";
        for(DingDingDepart de: list){
            a += de.getId()+ ","+de.getParentid() + "||";
        }
        System.out.println(a);
        Collections.sort(list);  
        a= "";
        for(DingDingDepart de: list){
            a += de.getId()+ ","+de.getParentid() + "||";
        }
        System.out.println(a);
    }
}
2017-09-29 15:59:22   Visit  117

使用Nginx做负载均衡时,在默认配置下Nginx默认使用的是轮流转发的模式,这次请求使用第一个服务,下次请求使用第二个服务。如果没有做session共享,就会出现session失效的问题。
比较简单的解决方法是使用 ip做过滤,根据ip来分配使用的服务,这样就保证一个用户会一直请求同一个服务。
配置如下

    upstream tomcat_server{
        ip_hash;
        server 127.0.0.1:8081  weight=1; 
        server 127.0.0.1:8082  weight=1; 
                server 127.0.0.1:8083  weight=1; 
    }

使用Nginx做负载均衡时base href 错误的问题

以下配置会产生base href 错误,造成使用 base href 的页面资源加载异常。

upstream tomcat_server{
     
         server 127.0.0.1:8081;
        
}
server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /  {
            proxy_pass http://tomcat_server;  
        }
}

只需要在 location 中添加以下设置,就会正常

location /  {
            proxy_pass http://tomcat_server; 
            proxy_set_header Host $http_host; 
            proxy_set_header X-Forward-For $remote_addr; 
          }
2017-08-31 14:03:53   Visit  229
select id, name
from  org
start with id = '1' 
connect by prior  id =  pid
©2017 Leechg.com