=()= / Говнокод #24884 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
#! /usr/bin/perl

use strict;
use warnings;

my %h1 = (one => 1, two => 2);
my %h2 = (three =>3 , four => 4);

sub h_uno { \%h1 }

sub h_multi {
    my %all = (%h1, %h2);

    \%all;
}

while (my ($k, $v) = each %{h_uno()}) {
    print "k=$k, v=$v\n";
}

# следующий цикл не завершится никогда
#while (my ($k, $v) = each %{h_multi()}) {
#    print "k=$k, $v=$v\n";
#}

Один из традиционных подколов собеседований на Perl вакансию.

https://www.linux.org.ru/forum/job/14518840

Elvenfighter Elvenfighter, (Updated )

Комментарии (55, +55)

=()= / Говнокод #23788 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
#!/usr/bin/env perl

use strict;
use warnings;
use utf8;
use diagnostics;

use LWP;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use JSON;
use HTML::TreeBuilder;
use HTML::FormatText;

my $ua = LWP::UserAgent->new();
$ua->agent('advice/0.9');

my $uri = 'http://fucking-great-advice.ru/api/random';

my $request = HTTP::Request->new('GET', $uri);
my $response = $ua->request($request);
my $content = from_json($response->content(), {utf8 => 1});

binmode(STDOUT, ':utf8');
my $tree = HTML::TreeBuilder->new->parse_content($content->{'text'});

my $formatter = HTML::FormatText->new(leftmargin => 0, rightmargin => 50);
print $formatter->format($tree);

Пользуйтесь на здоровье

Elvenfighter Elvenfighter, (Updated )

Комментарии (49, +49)

=()= / Говнокод #23437 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
sub PARSE_MULTIPART {
	my    ($STREAM,$boundary,$HANDLE,$PROCESS) = @_;
	open my $DH,'>',do{\(my $null)}; # devnull
	local $_ = "--\r\n";
	local $/ = \8192;
	my ($BR,$BL) = map {qr{^ (.*?) \Q$_\E }sx,length} map {"\r\n--$_"} $boundary;
	/^/;
	while ( (length) < length($_.=<$STREAM>) and $PROCESS->($.,${$/}) ){
			(print $DH
				m{$BR}
				       ? $1
				       : substr($_,0,-$BL,'')
			)
			 &&
				$+[0]
		 ...
				m{^
					    \r\n Content-Disposition       :\ form-data
					                                   ;\ name     = (?:"([^"]*?)"|(\S+?))
					(                                  ;\ filename = (?:"([^"]*?)"|(\S+?)) )?
					(?: \r\n Content-Type              :\            (.+?)                 )?
					(?: \r\n Content-Transfer-Encoding :\            (.+?)                 )?
					    \r\n
					    \r\n
				}sx
			 &&
				($DH = $HANDLE->({
					      'name'     => length $1 ? $1 : $2      ,
					$3 ? ('filename' => length $4 ? $4 : $5) : (),
					$6 ? ('type'     =>             $6     ) : (),
					$7 ? ('encode'   =>             $7     ) : (),
				}))
			 ||
				m{^--};
		$+[0] and substr($_,0,$+[0],''),/^/,redo;
	};
};

http://oleg-bunin.livejournal.com/28302.html?thread=369550

SemaReal SemaReal, (Updated )

Комментарии (4, +4)

=()= / Говнокод #23358 Ссылка на оригинал

0

  1. 1
print for <>

Базовая функциональность программы cat на perl. Кто короче?
(только возможности самого языка без exec и подобных, шеллы вне олимпиады)

Elvenfighter Elvenfighter, (Updated )

Комментарии (64, +64)

=()= / Говнокод #23249 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
if ( $MonitorMode eq \">=\" )
{
  if ( $NbrProcesses < $ProcNumber )
  {
    $Rule->Status(TRUE);
  }
}
elsif ( $MonitorMode eq \"<=\" )
{
  if ( $NbrProcesses > $ProcNumber )
  {
    $Rule->Status(TRUE);
  }
}
else
{
  if ( $NbrProcesses != $ProcNumber )
  {
    $Session->Value(\"PROCESSMODE\", \"\" );
    $Rule->Status(TRUE);
  }
};

Кровавый ентерпрайз. Кусок кода мейд бай ХулетПакард

rjhdby rjhdby, (Updated )

Комментарии (37, +37)

=()= / Говнокод #22429 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
#!/usr/bin/perl -w
# ESXi host CPU/memory plugin for Munin by Niels Engelen
# http://www.foonet.be
 
use strict;
use warnings;
use VMware::VIRuntime;
use VMware::VILib;
 
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
 
my $esxihost = basename($0);
$esxihost =~ s/esxi\_cpu\_mem\_//;
my $username = Opts::set_option ('username', $ENV{'username'});
my $password = Opts::set_option ('password', $ENV{'password'});
my $url = Opts::set_option ('url', "https://$esxihost/sdk/webService");
my ($host_info);
 
if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
  print "graph_title ESXi CPU/Memory ".$esxihost." \n";
  print "graph_args --base 1000 -r --lower-limit 0 --upper-limit 100 \n";
  print "graph_vlabel % \n";
  print "graph_category ESXi CPU/Memory\n";
  print "graph_scale no\n";
  print "mem.label MEM used\n";
  print "mem.draw AREA\n";
  print "cpu.label CPU used\n";
  exit 0;
} else {
  Opts::parse();
  Opts::validate();
  Util::connect();
  $host_info = Vim::find_entity_view(view_type => 'HostSystem', properties => ['summary.hardware', 'summary.quickStats'], filter => {'summary.runtime.connectionState' => 'connected'});
  &get_host_mem_info($host_info);
  &get_host_cpu_info($host_info);
  Util::disconnect();
  exit 0;
}
  
sub get_host_mem_info {
        if ($host_info) {
        my $percentMemoryUsed = ($host_info->{'summary.quickStats'}->overallMemoryUsage * 1024 * 1024 / $host_info->{'summary.hardware'}->memorySize) * 100;
        printf "mem.value %.2f\n",$percentMemoryUsed;
    }
}
  
sub get_host_cpu_info {
    if ($host_info) {
        my $percentCpuUsed = ($host_info->{'summary.quickStats'}->overallCpuUsage / ($host_info->{'summary.hardware'}->cpuMhz * $host_info->{'summary.hardware'}->numCpuCores)) * 100;
        printf "cpu.value %.2f\n",$percentCpuUsed;
        }
}

munin munin, (Updated )

Комментарии (15, +15)

=()= / Говнокод #22386 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
#!/usr/bin/env perl

# Parameters supported:
#
# config
# autoconf
#
# Magic markers:
#%# family=auto
#%# capabilities=autoconf

use strict;
use warnings;
use LWP;
use JSON qw/decode_json/;

=head1 NAME
elasticsearch_open_files - A munin plugin that collects the number of open files in your elasticsearch instances
=head1 APPLICABLE SYSTEMS
Elasticsearch
=head1 CONFIGURATION
None
=head1 BUGS
None known so far. If you find any, let me know.
=head1 AUTHOR
Kentaro Yoshida - https://github.com/y-ken
=cut

my $host = exists $ENV{'host'} ? $ENV{'host'} : 'localhost';
my $port = exists $ENV{'port'} ? $ENV{'port'} : 9200;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);

sub get_json_from_url {
    my $uri = shift;
    my $res = $ua->get($uri, 'Content-Type' => 'application/json' );
    Carp::confess($res->code . " for " . $uri) unless $res->is_success;
    my $data = do { local $@; eval { decode_json($res->content) } };
    die("Could not decode JSON from: " . $res->content) unless $data;
    return $data;
}

sub collect_max_file_descriptors {
    my $data = get_json_from_url("http://$host:$port/_nodes/_local/stats/process");
    foreach my $full_node_name (keys %{$data->{nodes}}) {
        return $data->{nodes}{$full_node_name}{process}{max_file_descriptors};
    }
}

sub collect_open_file_descriptors {
    my $data = get_json_from_url("http://$host:$port/_nodes/_local/stats/process");
    foreach my $full_node_name (keys %{$data->{nodes}}) {
        return $data->{nodes}{$full_node_name}{process}{open_file_descriptors};
    }
}

my %out = (
    max_file_descriptors => collect_max_file_descriptors,
    open_file_descriptors => collect_open_file_descriptors
);

if ($ARGV[0] and $ARGV[0] eq 'config') {
    print "graph_title Elasticsearch open files\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_vlabel number of open files\n";
    print "graph_category elasticsearch\n";
    print "open_file_descriptors.label open files\n";
    print "open_file_descriptors.type GAUGE\n";
    print "open_file_descriptors.info The number of currently open files.\n";
    print "max_file_descriptors.label max open files\n";
    print "max_file_descriptors.type GAUGE\n";
}
elsif (!$ARGV[0] || $ARGV[0] eq 'autoconf') {
    print "max_file_descriptors.value $out{max_file_descriptors}\n";
    print "open_file_descriptors.value $out{open_file_descriptors}\n";
}

exit(0);

munin munin, (Updated )

Комментарии (0)

=()= / Говнокод #22385 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
#!/usr/bin/env perl

# Parameters supported:
#
# config
# autoconf
#
# Magic markers:
#%# family=auto
#%# capabilities=autoconf

use strict;
use warnings;
use LWP;
use JSON qw/decode_json/;

=head1 NAME
elasticsearch_jvm_threads - A munin plugin that collects stats from the JVM of your elasticsearch instances
=head1 APPLICABLE SYSTEMS
Elasticsearch
=head1 CONFIGURATION
None
=head1 BUGS
None known so far. If you find any, let me know.
=head1 AUTHOR
Tomas Doran (t0m) - c<< <bobtfish@bobtfish.net> >>
Kentaro Yoshida - https://github.com/y-ken
=cut

my $host = exists $ENV{'host'} ? $ENV{'host'} : 'localhost';
my $port = exists $ENV{'port'} ? $ENV{'port'} : 9200;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);

sub get_json_from_url {
    my $uri = shift;
    my $res = $ua->get($uri, 'Content-Type' => 'application/json' );
    Carp::confess($res->code . " for " . $uri) unless $res->is_success;
    my $data = do { local $@; eval { decode_json($res->content) } };
    die("Could not decode JSON from: " . $res->content) unless $data;
    return $data;
}

my $data = get_json_from_url("http://$host:$port/_nodes/jvm");
my $t_data = get_json_from_url("http://$host:$port/_nodes/stats/jvm");
my %out = (count => 0, peak_count => 0);

foreach my $full_node_name (keys %{$data->{nodes}}) {
    next unless $t_data->{nodes}{$full_node_name};
    foreach my $name (keys %{ $t_data->{nodes}{$full_node_name}{jvm}{threads} }) {
        $out{$name} += $t_data->{nodes}{$full_node_name}{jvm}{threads}{$name};
    }
}
if ($ARGV[0] and $ARGV[0] eq 'config') {
    print "graph_title Elasticsearch JVM threads\n";
    print "graph_category elasticsearch\n";
    print "graph_scale no\n";

    foreach my $name (keys %out) {
        print "$name.label $name\n"."$name.type GAUGE\n";
    }
}
elsif (!$ARGV[0] || $ARGV[0] eq 'autoconf') {
    foreach my $name (keys %out) {
        print "$name.value " . $out{$name} . "\n";
    }
}

exit(0);

munin munin, (Updated )

Комментарии (0)