发布新日志

  • nload--linux下网络流量监控工具

    2010-06-25 17:00:10

    nload is a ncurse based network traffic analyser, displays the current network usage.

    nload是一个网络流量分析器,可对流入和流出的网络传输数据进行统计。

    1. 启动

    $nload

    默认情况下,nload将以kBit/s给出eth0网卡的流入和流出流量。

    2. 退出

    q or Ctrl+C

    3. 监控多个网络接口

    例如,监控eth0eth1lo设备,可以使用

    $nload eth0 eth1 lo

    监控时,每屏仅显示一个网络接口的流量情况,可以使用ArrowRight, ArrowDown, ArrowLeft, ArrowUp, PageUp, PageDown, Enter, Tab, n and p等键在不同网络接口之间切换。

    也可以使用-m选项使多个设备的流量状态显示在同一屏幕中。

    $nload -m eth0 eth1 lo

    4. 一些常用的选项

    改变更新数据的时间间隔: -t sss (sss is the time in milliseconds)。默认时间间隔是500ms

    改变流量数据的单位: -u h|b|k|m|g for human, bits, kilo, mega or giga bits

    改变流入和流出带宽的图形显示比例: -i XXX(XXXis given in kBits/s),; -o YYY

    流入流量的显示比例用-i改变,流出流量的用-o改变。默认值都是10240

  • bash脚本中出现[[:not found错误的解决方法

    2010-06-24 16:02:40

    今天在写脚本的时候,发生了一个奇怪的问题:在脚本中使用[[的时候报错“[[: not found”。遇到问题自然是解决问题。


    使用的bash版本太低?

    bash --version查看bash版本信息如下

    lee@lee:~$ bash --version

    GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)

    Copyright (C) 2007 Free Software Foundation, Inc.

    google bash手册,3.2.39已经不算低了,完全支持[[这样的扩展。看来不是版本问题。

    是脚本中[[使用错误?

    写测试脚本进行测试。test.sh测试脚本内容如下

    #!/bin/bash

    [[ 1 ]] && echo "successful"

    执行后仍然是“[[: not found”。但是,在bash交互模式下执行[[ 1 ]] && echo "successful"命令,却是成功的,执行结果如下

    lee@lee:~$ [[ 1 ]] && echo "successful"

    successful

    lee@lee:~$

    看来bash是支持[[扩展的,那么,问题应该就是出在脚本上。

    脚本里的问题存在于哪里呢?

    显然,那条孤零零的命令是没问题的,因为已经在交互模式下验证过了。脚本里还有一行#!/bin/bash

    用来指定运行该脚本所使用的shell类型。显然,我们这里就是要使用bash,所以这一行也没有问题。


    既然脚本的内容没有问题了,那问题究竟在哪里呢?

    从编写和运行等几个环节仔细思考,脚本既然没问题,那问题是不是出在 运行环节上?出于习惯,我经常喜欢$ sh test.sh这样的运行脚本的 方式,那么,换一种运行方式是不是能解决问题呢?在终端下用./test.sh运行,果然,运行成功!至此,问题的症结找到。


    下面的问题是,为什么sh test.sh./test.sh有着不同的运行结果。

    通过查看(ls -l /bin)得知,sh只是一个符号链接,最终指向是一个叫做dash的程序

    lee@lee:~$ ls -hl /bin | grep sh

    -rwxr-xr-x 1 root root 686K 2008-05-13 02:33 bash

    -rwxr-xr-x 1 root root  79K 2009-03-09 21:03 dash

    lrwxrwxrwx 1 root root    4 2010-03-03 01:52 rbash -> bash

    lrwxrwxrwx 1 root root    4 2010-03-03 01:53 sh -> dash

    lrwxrwxrwx 1 root root    4 2010-03-03 01:53 sh.distrib -> bash


    在运行sh test.sh时,首先调用sh命令,而sh指向dash,因此,sh test.sh相当于/bin/dash test.sh。而dash不管是名称还是程序大小,都与bash不同。那么,sh test.sh./test.sh两种命令有了不同的执行结果也就不足为奇。

    在执行./test.sh命令时,bash会自动生成一个subshell来执行该命令,即执行 filename arguments等 同于执行bash filename arguments


    还剩下的一个问题是,dashbash究竟有什么区别?

    Ubuntu wiki上给出了答案。自Ubuntu 6.10以后,系统的默认shell /bin/sh被改成了dashdash(the Debian Almquist shell) 是一个比bash小很多但仍兼容POSIX标准的shell,它占用的磁盘空间更少,执行shell脚本比bash更快,依赖的库文件更少,当然,在功能上无法与bash相比。dash来自于NetBSD版本的Almquist Shell(ash)

    Ubuntu中将默认shell改为dash的主要原因是效率。由于Ubuntu启动过程中需要启动大量的shell脚本,为了优化启动速度和资源使用情况,Ubuntu做了这样的改动。


    如何避免dash引起的问题?

    1. 如果dash仅仅只影响到几个shell脚本,则最方便的解决方法是修改脚本,在脚本中指定正确的shell解释器

    #!/bin/sh  改为  #!/bin/bash

    2. 如果dash影响到的是Makefile文件,则需要在文件开始指定以下变量

    SHELL = /bin/bash

    3. 如果被影响的范围较广,以至于修改单独的几个文件无法解决问题,此时 可以终止将dash安装为/bin/sh

    sudo dpkg-reconfigure dash

    需要注意的是,这样修改将会影响到系统的启动速度,甚至会影响到一些 依赖于dash独有特性的脚本(这些特性bash没有提供)。


    如何在编写脚本时避免这些影响?

    首先,可以使用checkbashisms命令检查脚本是否是“bash化”(bashism)的。要使用checkbashisms,需先安装devscripts

    aptitude install devscripts

    checkbashisms test.sh

    其次,在编写脚本时需要注意使用符合POSIX标准的语法,从而使脚本成为"POSIX shell"
  • umount时发生Device is busy的问题

    2010-05-27 13:39:43

    如果mount的 内容发生了变化,在访问mount内容时有可能出现僵死的情况,此时需要重新mount。

    mount -o remount dir

    将已mount的 只读状态的文件系统改为读写状态

    mount -o remount, rw dir

    该命令不会改变mount源 或mount点。


    umount
    在卸载设备时,有时会出现Device is busy的情况,导致umount进程一直僵在那里,无法继续。
    umount有两个选项可以尝试用来解决这个问题。

    umount -l /device  #Lazy umount. Detach the filesystem from the filesystem hierarchy now, and cleanup all references to the filesystem as soon as it is not busy anymore.
    umount -f /device  #Force unmount (in case of an unreachable NFS system)


    另外,fuser命 令在解决这种问题时很有用。

    fuser: identify processes using files or sockets
    fuser displays the PIDs of processes using the specified files or file systems.
    -m name specifies a file on a mounted file system or a block device that is mounted.
    fuser -km /home #kills all processes accessing the file system /home in any way.

  • mysql 数据库更改远程访问权限

    2010-05-27 13:19:38

    最近在调试mysql数据库时,发生以下错误

     

    18:04:45,575 ERROR [STDERR] org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not execute query; uncategorized SQLException for SQL [select productlib0_.servicelibvalueid as servicel1_7_, productlib0_.productid as productid7_, productlib0_.servicelibtypeid as servicel3_7_, productlib0_.servicelibid as servicel4_7_ from conferencemanagement.productlibtype productlib0_ where (productlib0_.productid=? )]; SQL state [HY000]; error code [1449]; There is no 'root'@'%' registered; nested exception is java.sql.SQLException: There is no 'root'@'%' registered

    18:04:45,577 ERROR [STDERR] Caused by:

    18:04:45,577 ERROR [STDERR] java.sql.SQLException: There is no 'root'@'%' registered

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)


    排查错误,最后发现是数据库远程访问权限的问题,使用以下命令即可解决。

    1. 登录mysql:mysql -uroot -ppasswd

    2. 授权:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION //授权任何主机均有访问数据库的权限。

    3. 重载权限信息:FLUSH PRIVILEGES //reload the grant tables

    4. 退出:quit

    另外,搜索到了其他修改方法,如下。

    修改mysql数据库的user表

    update user set host='%' where user = 'root';

    select host, user from user;

    有关mysql账户和权限管理的问题,可以参见以下文档

    http://dev.mysql.com/doc/refman/5.0/en/adding-users.html

  • Ubuntu系统如何安装双网卡及更改网卡名称(eth0改为eth1)

    2010-03-19 10:23:41

    最近,需要在Ubuntu系统上安装一块千兆网卡,使用到了一些网卡的安装、配置及更改操作。

    网卡可安装到任意一个PCI插槽,网卡型号D-Link DGE-530TUbuntu采用的是8.04 server版本。由于Ubuntu系统带有该网卡的驱动,因此,不必手动安装驱动。安装完成后进入系统,使用lspci查看是否有以下信息

    Ethernet controller: Intel Corporation 82562V-2 10/100 Network Connection

    Ethernet controller: D-Link System Inc DGE-530T Gigabit Ethernet Adapter

    其中,第一行所显示的网卡是系统原有的集成网卡,第二行所显示的网卡是新添加的D-Link网卡。如果仅有一行Ethernet信息,则说明新添加的网卡没有加载,需要手动安装驱动程序。

    新网卡添加后,即可对其进行配置。

    注意,此时新网卡的名称时eth1,因为eth0被原有的网卡占用。如果想将新网卡设置为eth0,则需要进行以下步骤。

    1ifconfig eth0ifconfig eth1查看网卡的MAC地址并记录,MAC地址主要用来区分两块网卡;

    2vi /etc/udev/rules.d/70-persistent-net.rules

    3)上面的文件内容与下面类似

    # This file was automatically generated by the /lib/udev/write_net_rules

    # program run by the persistent-net-generator.rules rules file.

    #

    # You can modify it, as long as you keep each rule on a single line.

     

    # PCI device 0x8086:0x10c0 (e1000e)

    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:1a:a0:9d:f2:58", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

     

    # PCI device 0x1186:0x4b01 (skge)

    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:26:5a:81:9f:e7", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

    其中,NAME="eth0"就是网卡的名称,该名称与MAC地址"00:1a:a0:9d:f2:58"对应。只要更改eth0eth1,就可以将MAC地址为"00:1a:a0:9d:f2:58"的网卡从eth0变成eth1.

    4)更改完后重启机器。

    如果无法分清百兆网卡和千兆网卡所对应的eth*,可以使用ethtool eth0ethtool eth1查看。如果ethtool eth0显示的信息类似与下面

    Settings for eth0:

    Supported ports: [ TP ]

    Supported link modes:   10baseT/Half 10baseT/Full

                            100baseT/Half 100baseT/Full

                            1000baseT/Half 1000baseT/Full

    Supports auto-negotiation: Yes

    Advertised link modes:  10baseT/Half 10baseT/Full

                            100baseT/Half 100baseT/Full

                            1000baseT/Half 1000baseT/Full

    说明eth0是千兆网卡,如果ethtool eth0显示的信息类似与下面

    Settings for eth0:

    Supported ports: [ TP ]

    Supported link modes:   10baseT/Half 10baseT/Full

                            100baseT/Half 100baseT/Full

    Supports auto-negotiation: Yes

    Advertised link modes:  10baseT/Half 10baseT/Full

                            100baseT/Half 100baseT/Full

    说明eth0是百兆网卡。

    对网络进行设置,可以编辑vi /etc/network/interfaces,类似与下

    # This file describes the network interfaces available on your system

    # and how to activate them. For more information, see interfaces(5).

    # The loopback network interface

    auto lo

    iface lo inet loopback

    auto eth0    #设置eth0

    iface eth0 inet static

        address 192.168.37.76

        netmask 255.255.255.0

        network 192.168.37.0

        broadcast 192.168.37.255

        gateway  192.168.37.254

        dns-nameservers 192.168.37.254

     

    #auto eth1    #设置eth1

    #iface eth1 inet static

    #address xxx

     

    #auto eth0:1    #单网卡设置多个IP

    #iface eth0:1 inet static

    #address 192.168.1.60

    #netmask 255.255.255.0

    #network x.x.x.x

    #broadcast x.x.x.x

    #gateway x.x.x.x

    设置完后重启网络

    /etc/init.d/networking restart
Open Toolbar