现在的位置: 首页 > 综合 > 正文

ci_sae 监控项目示例

2012年11月11日 ⁄ 综合 ⁄ 共 4456字 ⁄ 字号 评论关闭
一,楔子
SAE平台上的应用,使用的是PHP语言。这么好的环境,不禁手痒。做一个WEB监控来测试一下吧。
需求:设置一些URL和字符,定时去获取内容,验证字符。记录获取内容的用时,验证通过表示正常,验证不通过表示异常。
将会用到SAE的Cron,FetchURL和Mysql.

二,框架
内置应用的框架中,就ci_sae有过了解,那就用它了。
进入SAE->我的应用->应用向导,找到ci_sae,点击安装。会有一个弹窗向导,选择应用[ertong],选择版本[2],下一步。框架就安装好了。
运行SDK,把代码同步到本地。目录一般在这里:/sdk目录/apps/ertong/2
code 项目代码目录
config.yaml 项目配置信息
deploy.dat 好象是对比线上线下文件用的,放了些md5的信息。上传时,只上传更新过的文件。

三,升级
查看system\codeigniter\CodeIgniter.php,版本是1.7.2,在http://codeigniter.org.cn
/上版本是2.0.2,文档也更新了。反正两个版本都不熟,那就用新的吧。SAE上,mysql,本地文件的处理方式不一样,所以得对2.0做下升级。把
下载的1.7.2版的system/database下的文件copy到2.0下。更新application/config
/database.php,升级就完成了。就是这么简单。仔细对比下两版的文件,会发现sae版里的mysql的实现,换成了SaeMysql。这些就
交给CI去处理了。

四,建库
一张记录监控站点的信息,一张记录监控的日志。

CREATE TABLE IF NOT EXISTS `jk_site` (
`id`
bigint(20) NOT NULL AUTO_INCREMENT,
`name`
varchar(64) DEFAULT NULL,
`url`
varchar(256) DEFAULT NULL,
`category`
varchar(64) DEFAULT NULL,
`pattern_str`
varchar(64) DEFAULT NULL,
`state`
int(11) DEFAULT NULL ,
`last_modify_date`
datetime DEFAULT NULL,
`is_deleted`
int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ;

CREATE TABLE IF NOT EXISTS `jk_site_log` (
`id`
bigint(20) NOT NULL AUTO_INCREMENT,
`site_id`
bigint(20) DEFAULT NULL,
`name`
varchar(64) DEFAULT NULL,
`content_lenght`
int(11) DEFAULT NULL,
`elapsed_time`
double DEFAULT NULL,
`state`
int(11) DEFAULT NULL ,
`last_modify_date`
datetime DEFAULT NULL,
`is_deleted`
int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ;

用phpMyadmin对jk_site表写入一些数据。

五,查询
现在我们用ci来展示jk_site的数据。
先按MVC的目录,在application下分别建好对应的文件。
/controllers/site.php

class Site extends CI_Controller {
public function __construct()
{
parent
::__construct();
$this->load->helper('url');//控制URL
$this->load->library('pagination');//分页
}
public function index($offset = 0)
{
$this->load->model('site_model');
$data['query'] = $this->site_model->get();
$this->load->view('site_message',$data);
}
}

/models/site_model.php

class Site_model extends CI_Model {
function __construct()
{
parent
::__construct();
$this->load->database();//加载数据库
}

function get()
{
$this->db->from('site');//$db['default']['dbprefix'] = 'jk_'; 这个配置可以设置表前缀。
$query = $this->db->get();
return $query->result();
}
}

/views/site_message.php

<table class="list issues">
<tr>
<td>序号</td>
<td>名字</td>
<td>URL</td>
<td>状态</td>
<td>更新时间</td>
<td>操作</td>
</tr>
<?php foreach($query as $item): ?>
<tr>
<td><?php echo $item->id ?></td>
<td><?php echo $item->name ?></td>
<td><?php echo $item->url ?></td>
<td><?php echo $item->state ?></td>
<td><?php echo $item->last_modify_date ?></td>
<td>
<a href="<?php echo site_url('site/logs/'.$item->id) ?>">日志</a>
</td>
</tr>
<?php endforeach;?>

用SDK上传后,用http://2.ertong.sinaapp.com/index.php/site 进行访问,应该就能看到数据库里的数据了。

六,URLRewrite
SEA的环境不支持htaccess,不过它有替代方案。在config.yaml里写如下语句:

handle:
- rewrite:
if(!is_dir() && !is_file()) goto "index.php?%{QUERY_STRING}"

这样就可以用 http://2.ertong.sinaapp.com/site进行访问了。

七,定时器
在config.yaml里写如下语句:

cron:
- description: test
url: site
/jiankong
schedule: every
5 mins

则每5分钟会请求 http://ertong.sinaapp.com/site/jiankong 一次。注意仅对默认版本有效。

八,监控

function check_site($site)
{
//监控URL
$this->benchmark->mark('check_site_begin');
$content = $this->getUrl($site->url);
$this->benchmark->mark('check_site_end');

$findstr = iconv("GB2312","UTF-8//IGNORE",$site->pattern_str) ;
$state = strpos($content,$findstr)>0?0:1;

//记录监控日志
$data = array(
'site_id' => $site->id,
'name' => $site->name,
'content_lenght' => strlen($content),
'elapsed_time' => $this->benchmark->elapsed_time('check_site_begin', 'check_site_end'),
'state' => $state,
'last_modify_date' => date("Y-m-d H:i:s"),
'is_deleted' => 0
);
$this->db->insert('site_log', $data);
//更新站点状态
$data = array(
'state' => $state,
'last_modify_date' => date("Y-m-d H:i:s")
);
$this->db->where('id', $site->id);
$this->db->update('site', $data);
}

function getUrl($url)
{
$f = new SaeFetchurl();
$content = $f->fetch($url);
if($f->errno() == 0) return $content;
else return $f->errmsg();
}

SAE里通过$this->benchmark->mark进行计时,SaeFetchurl的使用也超简单。

九,分页
/controllers/site.php

public function logs($siteid = 0,$offset = 0)
{
$config['total_rows'] = 800;//切换成读取数据量。
$config['base_url'] = '/site/logs/'.$siteid;
$config['per_page'] = 20;//每页多少条
$config['uri_segment'] = 4;//页码的字段位置
$this->pagination->initialize($config);
$pagery = $this->pagination->create_links();
$per_page = $this->pagination->per_page;

$this->load->model('site_model');
$data['query'] = $this->site_model->getlog_pages($siteid,$per_page, $offset);
$data['pagery'] = $pagery;
$this->load->view('sitelog_message',$data);
}

/models/site_model.php

function getlog_pages($siteid=0,$num = 10, $offset = 0)
{
$this->db->from('site_log');
$this->db->where('site_id',$siteid);
$this->db->limit($num, $offset);
$this->db->order_by('id desc');
$query = $this->db->get();
return $query->result();
}

 
/views/sitelog_message.php

<div id="pager"><?php echo $pagery; ?><br class="clear" /></div>
【上篇】
【下篇】

抱歉!评论已关闭.