use LWP::UserAgent;
use Encode;
use Data::Dumper;
use JSON;
use POSIX;
sub check_es {
my $SDATE = strftime("%Y.%m.%d",localtime());
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");
#my $host = "http://10.5.100.232:9200/logstash-tlcb-2017.06.10/_search";
my $host = "http://192.168.137.2:9200/twitter/_search";
my $response = $ua->get($host);
$ua->default_headers;
if ($response->is_success) {
# print $response->decoded_content; # or whatever
my $r=$response->decoded_content;
my $r=encode_utf8($r);
$hash = decode_json($r);
}
else {
print $response->status_line;
} ;
my $count= $hash->{'hits'}{total};
return $count;
};
print check_es
C:UsersTLCBDesktopelkElasticsearch Api>perl check_es.pl
1
C:UsersTLCBDesktopelkElasticsearch Api>
在创建一个索引:
C:UsersTLCBDesktopelkElasticsearch Api>perl check_es.pl
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hit
s":{"total":2,"max_score":1.0,"hits":[{"_index":"twitter","_type":"tweet","_id":
"2","_score":1.0,"_source":{"post_date":"2009-11-15T14:12:12","user":"kimchy","m
essage":"trying out Elasticsearch999999"}},{"_index":"twitter","_type":"tweet","
_id":"1","_score":1.0,"_source":{"post_date":"2009-11-15T14:12:12","user":"kimch
y","message":"trying out Elasticsearch999999"}}]}}
2