Proc-ProcessTable-Colorizer

 view release on metacpan or  search on metacpan

lib/Proc/ProcessTable/Colorizer.pm  view on Meta::CPAN

			$values{pctmem} = 0;
		}
		if ( ! defined( $values{pctcpu} ) ){
			$values{pctcpu} = 0;
		}

		if ( ! defined( $values{size} ) ){
			$values{size} = 0;
		}

		$values{pctmem}=sprintf('%.2f', $values{pctmem});
		$values{pctcpu}=sprintf('%.2f', $values{pctcpu});

		$values{size}=$values{size}/1024;

		push( @procs, \%values );

	}

	#sort by CPU percent and then RAM
	@procs=sort {
		$a->{pctcpu} <=> $b->{pctcpu} or
			$a->{pctmem} <=> $b->{pctmem} or
			$a->{rss} <=> $b->{rss} or
			$a->{size} <=> $b->{size} or
			$a->{time} <=> $b->{time}
	} @procs;
	@procs=reverse(@procs);

	#put together the colored colums, minus the proc column which will be done later
	my @proc_column;
	foreach my $proc (@procs){
		my @line;
		$self->nextColorReset;

		my $show=0;

		#checks if it is the idle proc and if it should show it
		if (
			defined ( $proc->{idle} ) &&
			( ! $self->{showIdle} )
			){
			$show = 0;
		}else{
			my $required_hits=0; #number of hits required to print it
			my $hits=0; #default to zero so we print it unless we increment this for a search item

			#checks if we need to do a proc search
			my $proc_search=$self->{proc_search};
			if ( defined( $proc_search ) ){
				$required_hits++;
				#cehck if the cmndline or fname matches
				if ( $proc->{proc} =~ /$proc_search/ ){
					$hits++;
				}
			}

			#check to see if it needs to search for users
			my $user_search_array=$self->userSearchGet;
			if ( defined( $user_search_array->[0] ) ){
				my $user=getpwuid($proc->{uid});
				$required_hits++;
				my $user_search_int=0;
				my $matched=0;
				#search while we have a user defined and it has not already been matched
				while( 
					defined( $user_search_array->[ $user_search_int ] ) &&
					( $matched == 0 )
					){
					my $to_match=$user_search_array->[ $user_search_int ];
					my $to_invert=0;
					if ( $to_match=~ /^\!/ ){
						$to_invert=1;
						$to_match=~s/^\!//;
					}

					#check if it matches
					if ( $to_invert ){
						if ( $to_match ne $user ){
							$hits++;
							$matched=1;
						}
					}else{
						if ( $to_match eq $user ){
							$hits++;
							$matched=1;
						}
					}

					$user_search_int++;
				}
			}

			#check to see if it needs to search for wait channels
			my $wait_search_array=$self->waitSearchGet;
			if ( defined( $wait_search_array->[0] ) ){
				$required_hits++;
				my $wait_search_int=0;
				my $matched=0;
				#search while we have a wait channel defined and it has not already been matched
				while( 
					defined( $wait_search_array->[ $wait_search_int ] ) &&
					( $matched == 0 )
					){
					my $to_match=$wait_search_array->[ $wait_search_int ];
					my $to_invert=0;
					if ( $to_match=~ /^\!/ ){
						$to_invert=1;
						$to_match=~s/^\!//;
					}

					#check if it matches
					if ( $to_invert ){
						if ( $to_match ne $proc->{wchan} ){
							$hits++;
							$matched=1;
						}
					}else{
						if ( $to_match eq $proc->{wchan} ){
							$hits++;
							$matched=1;

lib/Proc/ProcessTable/Colorizer.pm  view on Meta::CPAN

				$required_hits++;
				if ( $proc->{state} eq 'zombie' ){
					$hits++;
				}
			}

			#show swapped out procs
			if ( $self->{swapped_out_search} ){
				$required_hits++;
				if (
					( $proc->{state} ne 'zombie' ) &&
					( $proc->{rss} == '0' )
					){
					$hits++;
				}
			}

			#checks to see if it should ignore its self
			my $self_ignore=$self->{self_ignore};
			if (
				#if it is set to 1
				( $self_ignore == 1 ) &&
				( $proc->{pid} == $$ )
				){
				$required_hits++;
			}elsif(
				#if it is set to 2... we only care if we are doing a search...
				#meaning required hits are greater than zero
				( $required_hits > 0 ) &&
				( $self_ignore == 2 ) &&
				( $proc->{pid} == $$ )
				){
				#increment this so it will always be off by one for this proc, meaning it is ignored
				$required_hits++;
			}

			if ( $required_hits == $hits ){
				$show=1;
			}
		}

		if (
			( $show )
			){

			foreach my $field ( @{$fields} ){
				my $item='';
				if ( defined( $proc->{$field} ) ){
					$item=$proc->{$field};
				}
				#we will add proc later once we know the size of the table
				if ($field ne 'proc'){
					if ( $field eq 'start' ){
						$item=$self->startString($item);
					}

					if (
						( $field eq 'uid' ) &&
						$self->{resolveUser}
						){
						$item=getpwuid($item);
					}

					#colorizes it
					if ( $field eq 'time' ){
						if ( $^O =~ 'linux' ){
							$item=$item/1000000;
						}
						$item=$self->timeString($item);
					}elsif( $field eq 'proc' ){
						$item=color($self->processColorGet).$item;
					}elsif( $field eq 'info'){
						my $left=$proc->{state};
						if ( 
							$left eq 'sleep' 
							){
							$left='S';
						}elsif(
							$left eq 'zombie'
							){
							$left='Z';
						}elsif(
							$left eq 'wait'
							){
							$left='W';
						}elsif(
							$left eq 'run'
						){
							$left='R';
						}

						#checks if it is swapped out
						if (
							( $proc->{state} ne 'zombie' ) &&
						( $proc->{rss} == '0' )
							){
							$left=$left.'O';
						}

						#waiting to exit
						if (
						( defined( $proc->{working_on_exiting} ) ) &&
							$proc->{working_on_exiting}
							){
							$left=$left.'E';
						}

						#session leader
						if (
							( defined( $proc->{is_session_leader} ) ) &&
							$proc->{is_session_leader}
							){
							$left=$left.'s';
						}

						#checks to see if any sort of locks are present
						if (
							( defined( $proc->{is_locked} ) || defined( $proc->{posix_advisory_lock} ) )&&
							( $proc->{is_locked} || $proc->{posix_advisory_lock} )
							){
							$left=$left.'L';



( run in 2.068 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )